Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> To keep it readable you may want to extract method, e.g:
* <pre class="code"><code class="java">
* verify(mock).addAll(<b>argThat(new IsListOfTwoElements())</b>);
* //becomes
* verify(mock).addAll(<b>listOfTwoElements()</b>);
* </code></pre>
*
* <b>Warning:</b> Be reasonable with using complicated argument matching, especially custom argument matchers, as it can make the test less readable.
* Sometimes it's better to implement equals() for arguments that are passed to mocks
* (Mockito naturally uses equals() for argument matching).
* This can make the test cleaner.
* <p>
* Also, <b>sometimes {@link ArgumentCaptor} may be a better fit</b> than custom matcher.
* //stubbing:
* when(mock.foo(anyVararg()).thenReturn(100);
*
* //prints 100
* System.out.println(mock.foo(1, 2));
* //also prints 100
* System.out.println(mock.foo(1, 2, 3, 4));
* </code></pre>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>null</code>.
*/
public static <T> T anyVararg() {
return (T) reportMatcher(AnyVararg.ANY_VARARG).returnNull();
}
public static <T> T isA(Class<T> clazz) {
return reportMatcher(new InstanceOf(clazz)).<T>returnFor(clazz);
}
/**
* <code>boolean</code> argument that is equal to the given value.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param value
* the given value.
* @return <code>0</code>.
*/
public static boolean eq(boolean value) {
return reportMatcher(new Equals(value)).returnFalse();
}
/**
* <code>byte</code> argument that is equal to the given value.
* <p>
* See examples in javadoc for {@link Matchers
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>) reportMatcher(new Same(value)).<T>returnFor(value);
}
/**
* <code>null</code> argument.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>null</code>.
*/
public static Object isNull() {
return reportMatcher(Null.NULL).returnNull();
}
/**
* <code>null</code> argument.
* The class argument is provided to avoid casting.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param clazz Type to avoid casting
* @return <code>null</code>.
*/
public static <T> T isNull(Class<T> clazz) {
return (T) reportMatcher(Null.NULL).returnNull();
}
/**
* Not <code>null</code> argument.
* <p>
* alias to {@link Matchers#isNotNull()}
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>null</code>.
*/
public static Object notNull() {
return reportMatcher(NotNull.NOT_NULL).returnNull();
}
/**
* Not <code>null</code> argument, not necessary of the given class.
* The class argument is provided to avoid casting.
* <p>
* alias to {@link Matchers#isNotNull(Class)}
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param clazz Type to avoid casting
* @return <code>null</code>.
*/
public static <T> T notNull(Class<T> clazz) {
return (T) reportMatcher(NotNull.NOT_NULL).returnNull();
}
/**
* Not <code>null</code> argument.
* <p>
* alias to {@link Matchers#notNull()}
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>null</code>.
*/
public static Object isNotNull() {
return notNull();
}
/**
* Not <code>null</code> argument, not necessary of the given class.
* The class argument is provided to
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> <p>
* See examples in javadoc for {@link ArgumentMatcher} class
*
* @param matcher decides whether argument matches
* @return <code>null</code>.
*/
public static <T> T argThat(Matcher<T> matcher) {
return reportMatcher(matcher).<T>returnNull();
}
/**
* Allows creating custom <code>Character</code> argument matchers.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param matcher decides whether argument matches
* @return <code>0</code>.
*/
public static char charThat(Matcher<Character> matcher) {
return reportMatcher(matcher).returnChar();
}
/**
* Allows creating custom <code>Boolean</code> argument matchers.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param matcher decides whether argument matches
* @return <code>false</code>.
*/
public static boolean booleanThat(Matcher<Boolean> matcher) {
return reportMatcher(matcher).returnFalse();
}
/**
* Allows creating custom <code>Byte</code> argument matchers.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param matcher decides whether argument matches
* @return <code>0</code>.
*/
public static byte byteThat(Matcher<Byte> matcher) {
return reportMatcher(matcher).returnZero();
}
/**
* Allows creating custom <code>Short</code> argument matchers.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param matcher decides whether argument matches
* @return <code>0</code>.
*/
public static short shortThat(Matcher<Short> matcher) {
return reportMatcher(matcher).returnZero();
}
/**
* Allows creating custom <code>Integer</code> argument matchers.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param matcher decides whether argument matches
* @return <code>0</code>.
*/
public static int intThat(Matcher<Integer> matcher) {
return reportMatcher(matcher).returnZero();
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> /**
* Allows creating custom <code>Long</code> argument matchers.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param matcher decides whether argument matches
* @return <code>0</code>.
*/
public static long longThat(Matcher<Long> matcher) {
return reportMatcher(matcher).returnZero();
}
/**
* Allows creating custom <code>Float</code> argument matchers.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param matcher decides whether argument matches
* @return <code>0</code>.
*/
public static float floatThat(Matcher<Float> matcher) {
return reportMatcher(matcher).returnZero();
}
/**
* Allows creating custom <code>Double</code> argument matchers.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @param matcher decides whether argument matches
* @return <code>0</code>.
*/
public static double doubleThat(Matcher<Double> matcher) {
return reportMatcher(matcher).returnZero();
}
private static HandyReturnValues reportMatcher(Matcher<?> matcher) {
return MOCKING_PROGRESS.getArgumentMatcherStorage().reportMatcher(matcher);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.util.reflection.GenericMaster;
import java.lang.reflect.Field;
/**
* Instantiate {@link ArgumentCaptor} a field annotated by @Captor.
*/
public class CaptorAnnotationProcessor implements FieldAnnotationProcessor<Captor> {
public Object process(Captor annotation, Field field) {
Class<?> type = field.getType();
if (!ArgumentCaptor.class.isAssignableFrom(type)) {
throw new MockitoException("@Captor field must be of the type ArgumentCaptor.\n" + "Field: '"
+ field.getName() + "' has wrong type\n"
+ "For info how to use @Captor annotations see examples in javadoc for MockitoAnnotations class.");
}
Class cls = new GenericMaster().getGenericType(field);
return ArgumentCaptor.forClass(cls);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers;
import java.io.Serializable;
import org.hamcrest.SelfDescribing;
public interface ContainsExtraTypeInformation extends Serializable {
SelfDescribing withExtraTypeInfo();
boolean typeMatches(Object object);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.verification.checkers;
import java.util.List;
import org.mockito.exceptions.Reporter;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.invocation.InvocationsFinder;
import org.mockito.internal.reporting.SmartPrinter;
import org.mockito.internal.verification.argumentmatching.ArgumentMatchingTool;
import org.mockito.invocation.Invocation;
public class MissingInvocationChecker {
private final Reporter reporter;
private final InvocationsFinder finder;
public MissingInvocationChecker() {
this(new InvocationsFinder(), new Reporter());
}
MissingInvocationChecker(InvocationsFinder finder, Reporter reporter) {
this.finder = finder;
this.reporter = reporter;
}
public void check(List<Invocation> invocations, InvocationMatcher wanted) {
List<Invocation> actualInvocations = finder.findInvocations(invocations, wanted);
if (actualInvocations.isEmpty()) {
Invocation similar = finder.findSimilarInvocation(invocations, wanted);
if (similar != null) {
ArgumentMatchingTool argumentMatchingTool = new ArgumentMatchingTool();
Integer[] indexesOfSuspiciousArgs = argumentMatchingTool.getSuspiciouslyNotMatchingArgsIndexes(wanted.getMatchers(), similar.getArguments());
SmartPrinter smartPrinter = new SmartPrinter(wanted, similar, indexesOfSuspiciousArgs);
reporter.argumentsAreDifferent(smartPrinter.getWanted(), smartPrinter.getActual(), similar.getLocation());
} else {
reporter.wantedButNotInvoked(wanted, invocations);
}
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers;
import java.io.Serializable;
import org.hamcrest.Description;
import org.mockito.ArgumentMatcher;
public class InstanceOf extends ArgumentMatcher<Object> implements Serializable {
private static final long serialVersionUID = 517358915876138366L;
private final Class<?> clazz;
public InstanceOf(Class<?> clazz) {
this.clazz = clazz;
}
public boolean matches(Object actual) {
return (actual != null) && clazz.isAssignableFrom(actual.getClass());
}
public void describeTo(Description description) {
description.appendText("isA(" + clazz.getName() + ")");
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.verification;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.verification.VerificationMode;
public class MockAwareVerificationMode implements VerificationMode {
private final Object mock;
private final VerificationMode mode;
public MockAwareVerificationMode(Object mock, VerificationMode mode) {
this.mock = mock;
this.mode = mode;
}
public void verify(VerificationData data) {
mode.verify(data);
}
public Object getMock() {
return mock;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> passed to mocks
* (Mockito naturally uses equals() for argument matching).
* This can make the test cleaner.
* <p>
* Also, <b>sometimes {@link ArgumentCaptor} may be a better fit</b> than custom matcher.
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
* <p>
* Read more about other matchers in javadoc for {@link Matchers} class
*
* @param <T> type of argument
*/
public abstract class ArgumentMatcher<T> extends BaseMatcher<T> {
private static final long serialVersionUID = -2145234737829370369L;
/**
* Returns whether this matcher accepts the given argument.
* <p>
* The method should <b>never</b> assert if the argument doesn't match. It
* should only return false.
*
* @param argument
* the argument
* @return whether this matcher accepts the given argument.
*/
public abstract boolean matches(Object argument);
/**
* By default this method decamelizes matchers name to promote meaningful names for matchers.
* <p>
* For example <b>StringWithStrongLanguage</b> matcher will generate 'String with strong language' description in case of failure.
* <p>
* You might want to override this method to
* provide more specific description of the matcher (useful when
* verification failures are reported).
*
* @param description the description to which the matcher description is
* appended.
*/
public void describeTo(Description description) {
String className = getClass().getSimpleName();
description.appendText(Decamelizer.decamelizeMatcher(className));
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation.realmethod;
import org.mockito.internal.creation.util.MockitoMethodProxy;
import org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter;
import java.io.Serializable;
/**
* Provides stack trace filtering on exception.
*/
public class CleanTraceRealMethod implements RealMethod, Serializable {
private static final long serialVersionUID = 3596550785818938496L;
private final RealMethod realMethod;
public CleanTraceRealMethod(MockitoMethodProxy methodProxy) {
this(new DefaultRealMethod(methodProxy));
}
public CleanTraceRealMethod(RealMethod realMethod) {
this.realMethod = realMethod;
}
public Object invoke(Object target, Object[] arguments) throws Throwable {
try {
return realMethod.invoke(target, arguments);
} catch (Throwable t) {
new ConditionalStackTraceFilter().filter(t);
throw t;
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.answers;
import org.mockito.exceptions.Reporter;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.Serializable;
/**
* Returns the passed parameter identity at specified index.
*
* <p>The <code>argumentIndex</code> represents the index in the argument array of the invocation.</p>
* <p>If this number equals -1 then the last argument is returned.</p>
*
* @see org.mockito.AdditionalAnswers
* @since 1.9.5
*/
public class ReturnsArgumentAt implements Answer<Object>, Serializable {
private static final long serialVersionUID = -589315085166295101L;
public static final int LAST_ARGUMENT = -1;
private final int wantedArgumentPosition;
/**
* Build the identity answer to return the argument at the given position in the argument array.
*
* @param wantedArgumentPosition The position of the argument identity to return in the invocation.
* Using <code>-1</code> indicates the last argument.
*/
public ReturnsArgumentAt(int wantedArgumentPosition) {
this.wantedArgumentPosition = checkWithinAllowedRange(wantedArgumentPosition);
}
public Object answer(InvocationOnMock invocation) throws Throwable {
validateIndexWithinInvocationRange(invocation);
return invocation.getArguments()[actualArgumentPosition(invocation)];
}
private int actualArgumentPosition(InvocationOnMock invocation) {
return returningLastArg() ?
lastArgumentIndexOf(invocation) :
argumentIndexOf(invocation);
}
private boolean returningLastArg() {
return wantedArgumentPosition == LAST_ARGUMENT;
}
private int argumentIndexOf(InvocationOnMock invocation) {
return wantedArgumentPosition;
}
private int lastArgumentIndexOf(InvocationOnMock invocation) {
return invocation.getArguments().length - 1;
}
private int checkWithinAllowedRange(int argumentPosition) {
if (argumentPosition != LAST_ARGUMENT && argumentPosition < 0) {
new Reporter().invalidArgumentRangeAtIdentityAnswerCreationTime();
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage;
import java.io.IOException;
import java.nio.charset.CharacterCodingException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface IMethods {
boolean booleanReturningMethod();
Boolean booleanObjectReturningMethod();
byte byteReturningMethod();
Byte byteObjectReturningMethod();
short shortReturningMethod();
Short shortObjectReturningMethod();
char charReturningMethod();
Character charObjectReturningMethod();
int intReturningMethod();
Integer integerReturningMethod();
long longReturningMethod();
Long longObjectReturningMethod();
float floatReturningMethod();
Float floatObjectReturningMethod();
double doubleReturningMethod();
Double doubleObjectReturningMethod();
Object objectReturningMethod(Object ... objects);
Object objectReturningMethodNoArgs();
String oneArg(boolean value);
String oneArg(Boolean value);
String forBoolean(Boolean value);
String oneArg(byte value);
String oneArg(Byte value);
String forByte(Byte value);
String oneArg(short value);
String oneArg(Short value);
String forShort(Short value);
String oneArg(char value);
String oneArg(Character value);
String forCharacter(Character value);
String oneArg(int value);
String oneArg(Integer value);
String forInteger(Integer value);
String oneArg(long value);
String oneArg(Long value);
String forLong(Long value);
String oneArg(float value);
String oneArg(Float value);
String forFloat(Float value);
String oneArg(double value);
String oneArg(Double value);
String forDouble(Double value);
String oneArg(Object value);
String oneArg(String value);
String throwsNothing(boolean value);
String throwsIOException(int count) throws IOException;
String throwsError(int count);
String simpleMethod();
String differentMethod();
String differentMethod(String argument);
String otherMethod();
String simpleMethod(String
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> argument);
String simpleMethod(Collection<?> collection);
String simpleMethod(Object argument);
String simpleMethod(int argument);
String simpleMethod(String argOne, Integer argTwo);
String simpleMethod(String one, Integer two, Integer three, Integer four, Integer five);
String simpleMethod(String one, String[] two);
Object threeArgumentMethod(int valueOne, Object valueTwo, String valueThree);
void twoArgumentMethod(int one, int two);
void arrayMethod(String[] strings);
String oneArray(boolean[] array);
String oneArray(byte[] array);
String oneArray(char[] array);
String oneArray(double[] array);
String oneArray(float[] array);
String oneArray(int[] array);
String oneArray(long[] array);
String oneArray(short[] array);
String oneArray(Object[] array);
String canThrowException() throws CharacterCodingException;
String oneArray(String[] array);
void varargsString(int i, String... string);
Object varargsObject(int i, Object... object);
void varargsbyte(byte... bytes);
int varargs(Object ... object);
String varargsReturningString(Object ... object);
int varargs(String ... string);
void mixedVarargs(Object i, String ... string);
List<String> listReturningMethod(Object ... objects);
LinkedList<String> linkedListReturningMethod();
String toString();
String toString(String foo);
void voidMethod();
String forList(List<String> list);
String forSet(Set<String> anySet);
String forMap(Map<String, String> map);
String forCollection(Collection<String> collection);
Object[] arrayReturningMethod();
IMethods iMethodsReturningMethod();
String stringReturningMethod();
Object objectArgMethod(Object str);
Object listArgMethod(List<String> list);
Object collectionArgMethod(Collection<String> collection);
Object setArgMethod(Set<String> set);
void longArg(long longArg);
void intArgumentMethod(int i);
int intArgumentReturningInt(int i);
boolean equals(String str);
boolean equals();
int hashCode(String str);
int toIntPrimitive(Integer i);
Integer toIntWrapper
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>(int i);
String forObject(Object object);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import org.mockito.Mock;
import org.mockito.MockSettings;
import org.mockito.Mockito;
import java.lang.reflect.Field;
/**
* Instantiates a mock on a field annotated by {@link Mock}
*/
public class MockAnnotationProcessor implements FieldAnnotationProcessor<Mock> {
public Object process(Mock annotation, Field field) {
MockSettings mockSettings = Mockito.withSettings();
if (annotation.extraInterfaces().length > 0) { // never null
mockSettings.extraInterfaces(annotation.extraInterfaces());
}
if ("".equals(annotation.name())) {
mockSettings.name(field.getName());
} else {
mockSettings.name(annotation.name());
}
if(annotation.serializable()){
mockSettings.serializable();
}
// see @Mock answer default value
mockSettings.defaultAnswer(annotation.answer().get());
return Mockito.mock(field.getType(), mockSettings);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
import org.mockito.exceptions.Reporter;
import org.mockito.internal.util.reflection.Constructors;
import org.mockito.mock.SerializableMode;
import java.io.Serializable;
import java.util.Collection;
@SuppressWarnings("unchecked")
public class MockCreationValidator {
private final MockUtil mockUtil = new MockUtil();
public void validateType(Class classToMock) {
if (!mockUtil.isTypeMockable(classToMock)) {
new Reporter().cannotMockFinalClass(classToMock);
}
}
public void validateExtraInterfaces(Class classToMock, Collection<Class> extraInterfaces) {
if (extraInterfaces == null) {
return;
}
for (Class i : extraInterfaces) {
if (classToMock == i) {
new Reporter().extraInterfacesCannotContainMockedType(classToMock);
}
}
}
public void validateMockedType(Class classToMock, Object spiedInstance) {
if (classToMock == null || spiedInstance == null) {
return;
}
if (!classToMock.equals(spiedInstance.getClass())) {
new Reporter().mockedTypeIsInconsistentWithSpiedInstanceType(classToMock, spiedInstance);
}
}
public void validateDelegatedInstance(Class classToMock, Object delegatedInstance) {
if (classToMock == null || delegatedInstance == null) {
return;
}
if (delegatedInstance.getClass().isAssignableFrom(classToMock)) {
new Reporter().mockedTypeIsInconsistentWithDelegatedInstanceType(classToMock, delegatedInstance);
}
}
public void validateSerializable(Class classToMock, boolean serializable) {
// We can't catch all the errors with this piece of code
// Having a **superclass that do not implements Serializable** might fail as well when serialized
// Though it might prevent issues when mockito is mocking a class without superclass.
if(serializable
&& !classToMock.isInterface()
&& !(Serializable.class.isAssignableFrom(classToMock))
&& Constructors.noArgConstructor
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util.collections;
import org.mockito.internal.util.Checks;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import static java.lang.reflect.Array.*;
/**
* hashCode and equals safe hash based set.
*
* <p>
* Useful for holding mocks that have un-stubbable hashCode or equals method,
* meaning that in this scenario the real code is always called and will most probably
* cause an {@link NullPointerException}.
* </p>
* <p>
* This collection wraps the mock in an augmented type {@link HashCodeAndEqualsMockWrapper}
* that have his own implementation.
* </p>
*
* @see HashCodeAndEqualsMockWrapper
*/
public class HashCodeAndEqualsSafeSet implements Set<Object> {
private final HashSet<HashCodeAndEqualsMockWrapper> backingHashSet = new HashSet<HashCodeAndEqualsMockWrapper>();
public Iterator<Object> iterator() {
return new Iterator<Object>() {
private final Iterator<HashCodeAndEqualsMockWrapper> iterator = backingHashSet.iterator();
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
return iterator.next().get();
}
public void remove() {
iterator.remove();
}
};
}
public int size() {
return backingHashSet.size();
}
public boolean isEmpty() {
return backingHashSet.isEmpty();
}
public boolean contains(Object mock) {
return backingHashSet.contains(HashCodeAndEqualsMockWrapper.of(mock));
}
public boolean add(Object mock) {
return backingHashSet.add(HashCodeAndEqualsMockWrapper.of(mock));
}
public boolean remove(Object mock) {
return backingHashSet.remove(HashCodeAndEqualsMockWrapper.of(mock));
}
public void clear() {
backingHashSet.clear();
}
@Override public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
@Override public boolean equals(Object o)
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> {
if (!(o instanceof HashCodeAndEqualsSafeSet)) {
return false;
}
HashCodeAndEqualsSafeSet that = (HashCodeAndEqualsSafeSet) o;
return backingHashSet.equals(that.backingHashSet);
}
@Override public int hashCode() {
return backingHashSet.hashCode();
}
public Object[] toArray() {
return unwrapTo(new Object[size()]);
}
private <T> T[] unwrapTo(T[] array) {
Iterator<Object> iterator = iterator();
for (int i = 0, objectsLength = array.length; i < objectsLength; i++) {
if (iterator.hasNext()) {
array[i] = (T) iterator.next();
}
}
return array;
}
public <T> T[] toArray(T[] typedArray) {
T[] array = typedArray.length >= size() ? typedArray :
(T[]) newInstance(typedArray.getClass().getComponentType(), size());
return unwrapTo(array);
}
public boolean removeAll(Collection<?> mocks) {
return backingHashSet.removeAll(asWrappedMocks(mocks));
}
public boolean containsAll(Collection<?> mocks) {
return backingHashSet.containsAll(asWrappedMocks(mocks));
}
public boolean addAll(Collection<?> mocks) {
return backingHashSet.addAll(asWrappedMocks(mocks));
}
public boolean retainAll(Collection<?> mocks) {
return backingHashSet.retainAll(asWrappedMocks(mocks));
}
private HashSet<HashCodeAndEqualsMockWrapper> asWrappedMocks(Collection<?> mocks) {
Checks.checkNotNull(mocks, "Passed collection should notify() be null");
HashSet<HashCodeAndEqualsMockWrapper> hashSet = new HashSet<HashCodeAndEqualsMockWrapper>();
for (Object mock : mocks) {
assert ! (mock instanceof HashCodeAndEqualsMockWrapper) : "WRONG";
hashSet.add(HashCodeAndEqualsMockWrapper.of(mock));
}
return hashSet;
}
@Override public String toString() {
return backingHashSet.toString();
}
public static HashCodeAndEqualsSafeSet of(Object... mocks) {
return of(Arrays.asList(mocks));
}
public static HashCodeAndEqualsSafeSet of(Iterable<Object> objects) {
HashCodeAndEqualsSafe
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>Set hashCodeAndEqualsSafeSet = new HashCodeAndEqualsSafeSet();
if (objects != null) {
for (Object mock : objects) {
hashCodeAndEqualsSafeSet.add(mock);
}
}
return hashCodeAndEqualsSafeSet;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>package org.mockito.internal.configuration.plugins;
import org.mockito.plugins.MockMaker;
import org.mockito.plugins.PluginSwitch;
import org.mockito.plugins.StackTraceCleanerProvider;
class PluginRegistry {
private final PluginSwitch pluginSwitch
= new PluginLoader(new DefaultPluginSwitch()).loadPlugin(PluginSwitch.class, DefaultPluginSwitch.class.getName());
private final MockMaker mockMaker
= new PluginLoader(pluginSwitch).loadPlugin(MockMaker.class, "org.mockito.internal.creation.cglib.CglibMockMaker");
private final StackTraceCleanerProvider stackTraceCleanerProvider
= new PluginLoader(pluginSwitch).loadPlugin(StackTraceCleanerProvider.class, "org.mockito.internal.exceptions.stacktrace.DefaultStackTraceCleanerProvider");
/**
* The implementation of the stack trace cleaner
*/
StackTraceCleanerProvider getStackTraceCleanerProvider() {
//TODO we should throw some sensible exception if this is null.
return stackTraceCleanerProvider;
}
/**
* Returns the implementation of the mock maker available for the current runtime.
*
* <p>Returns {@link org.mockito.internal.creation.cglib.CglibMockMaker} if no
* {@link org.mockito.plugins.MockMaker} extension exists or is visible in the current classpath.</p>
*/
MockMaker getMockMaker() {
return mockMaker;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations.Mock;
import java.lang.reflect.Field;
/**
* Instantiates a mock on a field annotated by {@link Mock}
*/
@SuppressWarnings("deprecation")
public class MockitoAnnotationsMockAnnotationProcessor implements FieldAnnotationProcessor<Mock> {
public Object process(Mock annotation, Field field) {
return Mockito.mock(field.getType(), field.getName());
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation;
import org.hamcrest.Matcher;
import org.mockito.internal.matchers.ArrayEquals;
import org.mockito.internal.matchers.Equals;
import org.mockito.internal.util.collections.ArrayUtils;
import java.util.ArrayList;
import java.util.List;
/**
* by Szczepan Faber, created at: 3/31/12
*/
public class ArgumentsProcessor {
// expands array varArgs that are given by runtime (1, [a, b]) into true
// varArgs (1, a, b);
public static Object[] expandVarArgs(final boolean isVarArgs, final Object[] args) {
if (!isVarArgs || new ArrayUtils().isEmpty(args) || args[args.length - 1] != null && !args[args.length - 1].getClass().isArray()) {
return args == null ? new Object[0] : args;
}
final int nonVarArgsCount = args.length - 1;
Object[] varArgs;
if (args[nonVarArgsCount] == null) {
// in case someone deliberately passed null varArg array
varArgs = new Object[] { null };
} else {
varArgs = ArrayEquals.createObjectArray(args[nonVarArgsCount]);
}
final int varArgsCount = varArgs.length;
Object[] newArgs = new Object[nonVarArgsCount + varArgsCount];
System.arraycopy(args, 0, newArgs, 0, nonVarArgsCount);
System.arraycopy(varArgs, 0, newArgs, nonVarArgsCount, varArgsCount);
return newArgs;
}
public static List<Matcher> argumentsToMatchers(Object[] arguments) {
List<Matcher> matchers = new ArrayList<Matcher>(arguments.length);
for (Object arg : arguments) {
if (arg != null && arg.getClass().isArray()) {
matchers.add(new ArrayEquals(arg));
} else {
matchers.add(new Equals(arg));
}
}
return matchers;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.defaultanswers;
import org.mockito.MockSettings;
import org.mockito.Mockito;
import org.mockito.internal.InternalMockHandler;
import org.mockito.internal.MockitoCore;
import org.mockito.internal.creation.settings.CreationSettings;
import org.mockito.internal.stubbing.InvocationContainerImpl;
import org.mockito.internal.stubbing.StubbedInvocationMatcher;
import org.mockito.internal.util.MockUtil;
import org.mockito.internal.util.reflection.GenericMetadataSupport;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.mock.MockCreationSettings;
import org.mockito.stubbing.Answer;
import java.io.IOException;
import java.io.Serializable;
import static org.mockito.Mockito.withSettings;
/**
* Returning deep stub implementation.
*
* Will return previously created mock if the invocation matches.
*
* <p>Supports nested generic information, with this answer you can write code like this :
*
* <pre class="code"><code class="java">
* interface GenericsNest<K extends Comparable<K> & Cloneable> extends Map<K, Set<Number>> {}
*
* GenericsNest<?> mock = mock(GenericsNest.class, new ReturnsGenericDeepStubs());
* Number number = mock.entrySet().iterator().next().getValue().iterator().next();
* </code></pre>
* </p>
*
* @see org.mockito.Mockito#RETURNS_DEEP_STUBS
* @see org.mockito.Answers#RETURNS_DEEP_STUBS
*/
public class ReturnsDeepStubs implements Answer<Object>, Serializable {
private static final long serialVersionUID = -7105341425736035847L;
public Object answer(InvocationOnMock invocation) throws Throwable {
GenericMetadataSupport returnTypeGenericMetadata =
actualParameterizedType(invocation.getMock()).resolveGenericReturnType
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>(invocation.getMethod());
Class<?> rawType = returnTypeGenericMetadata.rawType();
if (!mockitoCore().isTypeMockable(rawType)) {
return delegate().returnValueFor(rawType);
}
return deepStub(invocation, returnTypeGenericMetadata);
}
private Object deepStub(InvocationOnMock invocation, GenericMetadataSupport returnTypeGenericMetadata) throws Throwable {
InternalMockHandler<Object> handler = new MockUtil().getMockHandler(invocation.getMock());
InvocationContainerImpl container = (InvocationContainerImpl) handler.getInvocationContainer();
// matches invocation for verification
for (StubbedInvocationMatcher stubbedInvocationMatcher : container.getStubbedInvocations()) {
if (container.getInvocationForStubbing().matches(stubbedInvocationMatcher.getInvocation())) {
return stubbedInvocationMatcher.answer(invocation);
}
}
// record deep stub answer
return recordDeepStubAnswer(
newDeepStubMock(returnTypeGenericMetadata, invocation.getMock()),
container
);
}
/**
* Creates a mock using the Generics Metadata.
*
* <li>Finally as we want to mock the actual type, but we want to pass along the contextual generics meta-data
* that was resolved for the current return type, for this to happen we associate to the mock an new instance of
* {@link ReturnsDeepStubs} answer in which we will store the returned type generic metadata.
*
* @param returnTypeGenericMetadata The metadata to use to create the new mock.
* @param parentMock The parent of the current deep stub mock.
* @return The mock
*/
private Object newDeepStubMock(GenericMetadataSupport returnTypeGenericMetadata, Object parentMock) {
MockCreationSettings parentMockSettings = new MockUtil().getMockSettings(parentMock);
return mockitoCore().mock(
returnTypeGenericMetadata.rawType(),
withSettingsUsing(returnTypeGenericMetadata, parentMockSettings)
);
}
private MockSettings withSettingsUsing(GenericMetadataSupport returnTypeGenericMetadata, MockCreationSettings parentMockSettings) {
MockSettings mockSettings = returnTypeGenericMetadata.hasRawExtraInterfaces() ?
withSettings().extraInterfaces(returnTypeGenericMetadata.rawExtraInterfaces())
: withSettings();
return propagateSerializationSettings(mockSettings, parentMockSettings)
.defaultAnswer(returnsDeepStubsAnswerUsing(returnTypeGenericMetadata));
}
private MockSettings
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> propagateSerializationSettings(MockSettings mockSettings, MockCreationSettings parentMockSettings) {
return mockSettings.serializable(parentMockSettings.getSerializableMode());
}
private ReturnsDeepStubs returnsDeepStubsAnswerUsing(final GenericMetadataSupport returnTypeGenericMetadata) {
return new ReturnsDeepStubsSerializationFallback(returnTypeGenericMetadata);
}
private Object recordDeepStubAnswer(final Object mock, InvocationContainerImpl container) throws Throwable {
container.addAnswer(new DeeplyStubbedAnswer(mock), false);
return mock;
}
protected GenericMetadataSupport actualParameterizedType(Object mock) {
CreationSettings mockSettings = (CreationSettings) new MockUtil().getMockHandler(mock).getMockSettings();
return GenericMetadataSupport.inferFrom(mockSettings.getTypeToMock());
}
private static class ReturnsDeepStubsSerializationFallback extends ReturnsDeepStubs implements Serializable {
@SuppressWarnings("serial") // not gonna be serialized
private final GenericMetadataSupport returnTypeGenericMetadata;
public ReturnsDeepStubsSerializationFallback(GenericMetadataSupport returnTypeGenericMetadata) {
this.returnTypeGenericMetadata = returnTypeGenericMetadata;
}
@Override
protected GenericMetadataSupport actualParameterizedType(Object mock) {
return returnTypeGenericMetadata;
}
private Object writeReplace() throws IOException {
return Mockito.RETURNS_DEEP_STUBS;
}
}
private static class DeeplyStubbedAnswer implements Answer<Object>, Serializable {
@SuppressWarnings("serial") // serialization will fail with a nice message if mock not serializable
private final Object mock;
DeeplyStubbedAnswer(Object mock) {
this.mock = mock;
}
public Object answer(InvocationOnMock invocation) throws Throwable {
return mock;
}
}
private static MockitoCore mockitoCore() {
return LazyHolder.MOCKITO_CORE;
}
private static ReturnsEmptyValues delegate() {
return LazyHolder.DELEGATE;
}
private static class LazyHolder {
private static final MockitoCore MOCKITO_CORE = new MockitoCore();
private static final ReturnsEmptyValues DELEGATE = new ReturnsEmptyValues();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.cglib;
import org.mockito.cglib.proxy.MethodProxy;
import org.mockito.internal.creation.util.MockitoMethodProxy;
class DelegatingMockitoMethodProxy implements MockitoMethodProxy {
private final MethodProxy methodProxy;
public DelegatingMockitoMethodProxy(MethodProxy methodProxy) {
this.methodProxy = methodProxy;
}
public Object invokeSuper(Object target, Object[] arguments) throws Throwable {
return methodProxy.invokeSuper(target, arguments);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection.scanner;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.exceptions.Reporter;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
/**
* Scan field for injection.
*/
@SuppressWarnings("deprecation")
public class InjectMocksScanner {
private final Class<?> clazz;
/**
* Create a new InjectMocksScanner for the given clazz on the given instance
*
* @param clazz Current class in the hierarchy of the test
*/
public InjectMocksScanner(Class<?> clazz) {
this.clazz = clazz;
}
/**
* Add the fields annotated by @{@link InjectMocks}
*
* @param mockDependentFields Set of fields annotated by @{@link InjectMocks}
*/
public void addTo(Set<Field> mockDependentFields) {
mockDependentFields.addAll(scan());
}
/**
* Scan fields annotated by @InjectMocks
*
* @return Fields that depends on Mock
*/
private Set<Field> scan() {
Set<Field> mockDependentFields = new HashSet<Field>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (null != field.getAnnotation(InjectMocks.class)) {
assertNoAnnotations(field, Mock.class, MockitoAnnotations.Mock.class, Captor.class);
mockDependentFields.add(field);
}
}
return mockDependentFields;
}
void assertNoAnnotations(final Field field, final Class... annotations) {
for (Class annotation : annotations) {
if (field.isAnnotationPresent(annotation)) {
new Reporter().unsupportedCombinationOfAnnotations(annotation.getSimpleName(), InjectMocks.class.getSimpleName());
}
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.answers;
import org.mockito.Answers;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.Serializable;
import java.lang.reflect.Modifier;
/**
* Optional Answer that adds partial mocking support
* <p>
* {@link Answer} can be used to define the return values of unstubbed invocations.
* <p>
* This implementation can be helpful when working with legacy code.
* When this implementation is used, unstubbed methods will delegate to the real implementation.
* This is a way to create a partial mock object that calls real methods by default.
* <p>
* As usual you are going to read <b>the partial mock warning</b>:
* Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy objects.
* How does partial mock fit into this paradigm? Well, it just doesn't...
* Partial mock usually means that the complexity has been moved to a different method on the same object.
* In most cases, this is not the way you want to design your application.
* <p>
* However, there are rare cases when partial mocks come handy:
* dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.)
* However, I wouldn't use partial mocks for new, test-driven & well-designed code.
* <p>
*/
public class CallsRealMethods implements Answer<Object>, Serializable {
private static final long serialVersionUID = 9057165148930624087L;
public Object answer(InvocationOnMock invocation) throws Throwable {
if (Modifier.isAbstract(invocation.getMethod().getModifiers())) {
return Answers.RETURNS_DEFAULTS.get().answer(invocation);
}
return invocation.callRealMethod();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.listeners;
import org.mockito.exceptions.PrintableInvocation;
import org.mockito.invocation.DescribedInvocation;
/**
* Represent a method call on a mock.
*
* <p>
* Contains the information on the mock, the location of the stub
* the return value if it returned something (maybe null), or an
* exception if one was thrown when the method was invoked.
* </p>
*/
public interface MethodInvocationReport {
/**
* The return type is deprecated, please assign the return value from this method
* to the {@link DescribedInvocation} type. Sorry for inconvenience but we had to move
* {@link PrintableInvocation} to better place to keep the API consistency.
*
* @return Information on the method call, never {@code null}
*/
DescribedInvocation getInvocation();
/**
* @return The resulting value of the method invocation, may be <code>null</code>
*/
Object getReturnedValue();
/**
* @return The throwable raised by the method invocation, maybe <code>null</code>
*/
Throwable getThrowable();
/**
* @return <code>true</code> if an exception was raised, <code>false</code> otherwise
*/
boolean threwException();
/**
* @return Location of the stub invocation
*/
String getLocationOfStubbing();
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection.filter;
import java.lang.reflect.Field;
import java.util.Collection;
public interface MockCandidateFilter {
OngoingInjecter filterCandidate(
Collection<Object> mocks,
Field fieldToBeInjected,
Object fieldInstance
);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>bing.getInvocation();
mockingProgress.stubbingCompleted(invocation);
AnswersValidator answersValidator = new AnswersValidator();
answersValidator.validate(answer, invocation);
synchronized (stubbed) {
if (isConsecutive) {
stubbed.getFirst().addAnswer(answer);
} else {
stubbed.addFirst(new StubbedInvocationMatcher(invocationForStubbing, answer));
}
}
}
Object answerTo(Invocation invocation) throws Throwable {
return findAnswerFor(invocation).answer(invocation);
}
public StubbedInvocationMatcher findAnswerFor(Invocation invocation) {
synchronized (stubbed) {
for (StubbedInvocationMatcher s : stubbed) {
if (s.matches(invocation)) {
s.markStubUsed(invocation);
invocation.markStubbed(new StubInfoImpl(s));
return s;
}
}
}
return null;
}
public void addAnswerForVoidMethod(Answer answer) {
answersForStubbing.add(answer);
}
public void setAnswersForStubbing(List<Answer> answers) {
answersForStubbing.addAll(answers);
}
public boolean hasAnswersForStubbing() {
return !answersForStubbing.isEmpty();
}
public boolean hasInvocationForPotentialStubbing() {
return !registeredInvocations.isEmpty();
}
public void setMethodForStubbing(InvocationMatcher invocation) {
invocationForStubbing = invocation;
assert hasAnswersForStubbing();
for (int i = 0; i < answersForStubbing.size(); i++) {
addAnswer(answersForStubbing.get(i), i != 0);
}
answersForStubbing.clear();
}
@Override
public String toString() {
return "invocationForStubbing: " + invocationForStubbing;
}
public List<Invocation> getInvocations() {
return registeredInvocations.getAll();
}
public List<StubbedInvocationMatcher> getStubbedInvocations() {
return stubbed;
}
public Object invokedMock() {
return invocationForStubbing.getInvocation().getMock();
}
public InvocationMatcher getInvocationForStubbing() {
return invocationForStubbing;
}
private RegisteredInvocations createRegisteredInvocations(MockCreationSettings mockSettings) {
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing;
import java.io.Serializable;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.invocation.DescribedInvocation;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
@SuppressWarnings("unchecked")
public class StubbedInvocationMatcher extends InvocationMatcher implements Answer, Serializable {
private static final long serialVersionUID = 4919105134123672727L;
private final Queue<Answer> answers = new ConcurrentLinkedQueue<Answer>();
private DescribedInvocation usedAt;
public StubbedInvocationMatcher(InvocationMatcher invocation, Answer answer) {
super(invocation.getInvocation(), invocation.getMatchers());
this.answers.add(answer);
}
public Object answer(InvocationOnMock invocation) throws Throwable {
//see ThreadsShareGenerouslyStubbedMockTest
Answer a;
synchronized(answers) {
a = answers.size() == 1 ? answers.peek() : answers.poll();
}
return a.answer(invocation);
}
public void addAnswer(Answer answer) {
answers.add(answer);
}
public void markStubUsed(DescribedInvocation usedAt) {
this.usedAt = usedAt;
}
public boolean wasUsed() {
return usedAt != null;
}
@Override
public String toString() {
return super.toString() + " stubbed with: " + answers;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.util.MockUtil;
import org.mockito.internal.util.reflection.FieldReader;
import org.mockito.internal.util.reflection.FieldSetter;
import java.lang.reflect.Field;
import java.util.Set;
import static org.mockito.Mockito.withSettings;
/**
* Handler for field annotated with @InjectMocks and @Spy.
*
* <p>
* The handler assumes that field initialization AND injection already happened.
* So if the field is still null, then nothing will happen there.
* </p>
*/
public class SpyOnInjectedFieldsHandler extends MockInjectionStrategy {
@Override
protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
FieldReader fieldReader = new FieldReader(fieldOwner, field);
// TODO refoctor : code duplicated in SpyAnnotationEngine
if(!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) {
try {
Object instance = fieldReader.read();
if (new MockUtil().isMock(instance)) {
// A. instance has been spied earlier
// B. protect against multiple use of MockitoAnnotations.initMocks()
Mockito.reset(instance);
} else {
new FieldSetter(fieldOwner, field).set(
Mockito.mock(instance.getClass(), withSettings()
.spiedInstance(instance)
.defaultAnswer(Mockito.CALLS_REAL_METHODS)
.name(field.getName()))
);
}
} catch (Exception e) {
throw new MockitoException("Problems initiating spied field " + field.getName(), e);
}
}
return false;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.defaultanswers;
import java.io.Serializable;
import org.mockito.configuration.IMockitoConfiguration;
import org.mockito.internal.configuration.GlobalConfiguration;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/**
* Globally configured Answer.
* <p>
* See javadoc for {@link IMockitoConfiguration}
*/
public class GloballyConfiguredAnswer implements Answer<Object>, Serializable {
private static final long serialVersionUID = 3585893470101750917L;
public Object answer(InvocationOnMock invocation) throws Throwable {
return new GlobalConfiguration().getDefaultAnswer().answer(invocation);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>RETURNS_MOCKS
*/
RETURNS_MOCKS(new ReturnsMocks()),
/**
* An answer that returns <strong>deep stubs</strong> (not mocks).
*
* <p>Please see the {@link org.mockito.Mockito#RETURNS_DEEP_STUBS} documentation for more details.</p>
*
* @see org.mockito.Mockito#RETURNS_DEEP_STUBS
*/
RETURNS_DEEP_STUBS(new ReturnsDeepStubs()),
/**
* An answer that calls the real methods (used for partial mocks).
*
* <p>Please see the {@link org.mockito.Mockito#CALLS_REAL_METHODS} documentation for more details.</p>
*
* @see org.mockito.Mockito#CALLS_REAL_METHODS
*/
CALLS_REAL_METHODS(new CallsRealMethods())
;
private final Answer<Object> implementation;
private Answers(Answer<Object> implementation) {
this.implementation = implementation;
}
public Answer<Object> get() {
return implementation;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util.reflection;
import org.mockito.exceptions.base.MockitoException;
import java.lang.reflect.Field;
public class FieldReader {
final Object target;
final Field field;
final AccessibilityChanger changer = new AccessibilityChanger();
public FieldReader(Object target, Field field) {
this.target = target;
this.field = field;
changer.enableAccess(field);
}
public boolean isNull() {
return read() == null;
}
public Object read() {
try {
return field.get(target);
} catch (Exception e) {
throw new MockitoException("Cannot read state from field: " + field + ", on instance: " + target);
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.invocation;
import java.io.Serializable;
import java.lang.reflect.Method;
/**
* An invocation on a mock
* <p>
* A placeholder for mock, the method that was called and the arguments that were passed.
*/
public interface InvocationOnMock extends Serializable {
/**
* returns the mock object
*
* @return mock object
*/
Object getMock();
/**
* returns the method
*
* @return method
*/
Method getMethod();
/**
* returns arguments passed to the method
*
* @return arguments
*/
Object[] getArguments();
/**
* Returns casted argument using position
* @param index argument position
* @param clazz argument type
* @return casted argument on position
*/
<T> T getArgumentAt(int index, Class<T> clazz);
/**
* calls real method
* <p>
* <b>Warning:</b> depending on the real implementation it might throw exceptions
*
* @return whatever the real method returns / throws
* @throws Throwable in case real method throws
*/
Object callRealMethod() throws Throwable;
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers;
public interface CapturesArguments {
void captureFrom(Object argument);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>();
public boolean isTypeMockable(Class<?> typeToMock) {
return mockUtil.isTypeMockable(typeToMock);
}
public <T> T mock(Class<T> typeToMock, MockSettings settings) {
if (!MockSettingsImpl.class.isInstance(settings)) {
throw new IllegalArgumentException(
"Unexpected implementation of '" + settings.getClass().getCanonicalName() + "'\n"
+ "At the moment, you cannot provide your own implementations that class.");
}
MockSettingsImpl impl = MockSettingsImpl.class.cast(settings);
MockCreationSettings<T> creationSettings = impl.confirm(typeToMock);
T mock = mockUtil.createMock(creationSettings);
mockingProgress.mockingStarted(mock, typeToMock);
return mock;
}
public IOngoingStubbing stub() {
IOngoingStubbing stubbing = mockingProgress.pullOngoingStubbing();
if (stubbing == null) {
mockingProgress.reset();
reporter.missingMethodInvocation();
}
return stubbing;
}
public <T> DeprecatedOngoingStubbing<T> stub(T methodCall) {
mockingProgress.stubbingStarted();
return (DeprecatedOngoingStubbing) stub();
}
public <T> OngoingStubbing<T> when(T methodCall) {
mockingProgress.stubbingStarted();
return (OngoingStubbing) stub();
}
public <T> T verify(T mock, VerificationMode mode) {
if (mock == null) {
reporter.nullPassedToVerify();
} else if (!mockUtil.isMock(mock)) {
reporter.notAMockPassedToVerify(mock.getClass());
}
mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, mode));
return mock;
}
public <T> void reset(T ... mocks) {
mockingProgress.validateState();
mockingProgress.reset();
mockingProgress.resetOngoingStubbing();
for (T m : mocks) {
mockUtil.resetMock(m);
}
}
public void verifyNoMoreInteractions(Object... mocks) {
assertMocksNotEmpty(mocks);
mockingProgress.validateState();
for (Object mock : mocks
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>) {
try {
if (mock == null) {
reporter.nullPassedToVerifyNoMoreInteractions();
}
InvocationContainer invocations = mockUtil.getMockHandler(mock).getInvocationContainer();
VerificationDataImpl data = new VerificationDataImpl(invocations, null);
VerificationModeFactory.noMoreInteractions().verify(data);
} catch (NotAMockException e) {
reporter.notAMockPassedToVerifyNoMoreInteractions();
}
}
}
public void verifyNoMoreInteractionsInOrder(List<Object> mocks, InOrderContext inOrderContext) {
mockingProgress.validateState();
VerifiableInvocationsFinder finder = new VerifiableInvocationsFinder();
VerificationDataInOrder data = new VerificationDataInOrderImpl(inOrderContext, finder.find(mocks), null);
VerificationModeFactory.noMoreInteractions().verifyInOrder(data);
}
private void assertMocksNotEmpty(Object[] mocks) {
if (mocks == null || mocks.length == 0) {
reporter.mocksHaveToBePassedToVerifyNoMoreInteractions();
}
}
public InOrder inOrder(Object... mocks) {
if (mocks == null || mocks.length == 0) {
reporter.mocksHaveToBePassedWhenCreatingInOrder();
}
for (Object mock : mocks) {
if (mock == null) {
reporter.nullPassedWhenCreatingInOrder();
} else if (!mockUtil.isMock(mock)) {
reporter.notAMockPassedWhenCreatingInOrder();
}
}
return new InOrderImpl(Arrays.asList(mocks));
}
public Stubber doAnswer(Answer answer) {
mockingProgress.stubbingStarted();
mockingProgress.resetOngoingStubbing();
return new StubberImpl().doAnswer(answer);
}
public <T> VoidMethodStubbable<T> stubVoid(T mock) {
InternalMockHandler<T> handler = mockUtil.getMockHandler(mock);
mockingProgress.stubbingStarted();
return handler.voidMethodStubbable(mock);
}
public void validateMockitoUsage() {
mockingProgress.validateState();
}
/**
* For testing purposes only. Is not the part of main API.
* @return last invocation
*/
public Invocation getLastInvocation() {
Ongo
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>ingStubbingImpl ongoingStubbing = ((OngoingStubbingImpl) mockingProgress.pullOngoingStubbing());
List<Invocation> allInvocations = ongoingStubbing.getRegisteredInvocations();
return allInvocations.get(allInvocations.size()-1);
}
public Object[] ignoreStubs(Object... mocks) {
for (Object m : mocks) {
InvocationContainer invocationContainer = new MockUtil().getMockHandler(m).getInvocationContainer();
List<Invocation> ins = invocationContainer.getInvocations();
for (Invocation in : ins) {
if (in.stubInfo() != null) {
in.ignoreForVerification();
}
}
}
return mocks;
}
public MockingDetails mockingDetails(Object toInspect) {
return new DefaultMockingDetails(toInspect, new MockUtil());
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import org.mockito.*;
import org.mockito.configuration.AnnotationEngine;
import org.mockito.internal.configuration.injection.scanner.InjectMocksScanner;
import org.mockito.internal.configuration.injection.scanner.MockScanner;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
import static org.mockito.internal.util.collections.Sets.newMockSafeHashSet;
/**
* See {@link MockitoAnnotations}
*/
@SuppressWarnings({"deprecation", "unchecked"})
public class InjectingAnnotationEngine implements AnnotationEngine {
private final AnnotationEngine delegate = new DefaultAnnotationEngine();
private final AnnotationEngine spyAnnotationEngine = new SpyAnnotationEngine();
/***
* Create a mock using {@link DefaultAnnotationEngine}
*
* @see org.mockito.internal.configuration.DefaultAnnotationEngine
* @see org.mockito.configuration.AnnotationEngine#createMockFor(java.lang.annotation.Annotation, java.lang.reflect.Field)
*/
@Deprecated
public Object createMockFor(Annotation annotation, Field field) {
return delegate.createMockFor(annotation, field);
}
/**
* Process the fields of the test instance and create Mocks, Spies, Captors and inject them on fields
* annotated @InjectMocks.
*
* <p>
* This code process the test class and the super classes.
* <ol>
* <li>First create Mocks, Spies, Captors.</li>
* <li>Then try to inject them.</li>
* </ol>
*
* @param clazz Not used
* @param testInstance The instance of the test, should not be null.
*
* @see org.mockito.configuration.AnnotationEngine#process(Class, Object)
*/
public void process(Class<?> clazz, Object testInstance) {
processIndependentAnnotations(testInstance.getClass(), testInstance);
processInjectMocks(testInstance.getClass(), testInstance);
}
private void processInjectMocks(final Class<?> clazz, final Object
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> testInstance) {
Class<?> classContext = clazz;
while (classContext != Object.class) {
injectMocks(testInstance);
classContext = classContext.getSuperclass();
}
}
private void processIndependentAnnotations(final Class<?> clazz, final Object testInstance) {
Class<?> classContext = clazz;
while (classContext != Object.class) {
//this will create @Mocks, @Captors, etc:
delegate.process(classContext, testInstance);
//this will create @Spies:
spyAnnotationEngine.process(classContext, testInstance);
classContext = classContext.getSuperclass();
}
}
/**
* Initializes mock/spies dependencies for objects annotated with
* @InjectMocks for given testClassInstance.
* <p>
* See examples in javadoc for {@link MockitoAnnotations} class.
*
* @param testClassInstance
* Test class, usually <code>this</code>
*/
public void injectMocks(final Object testClassInstance) {
Class<?> clazz = testClassInstance.getClass();
Set<Field> mockDependentFields = new HashSet<Field>();
Set<Object> mocks = newMockSafeHashSet();
while (clazz != Object.class) {
new InjectMocksScanner(clazz).addTo(mockDependentFields);
new MockScanner(testClassInstance, clazz).addPreparedMocks(mocks);
clazz = clazz.getSuperclass();
}
new DefaultInjectionEngine().injectMocksOnFields(mockDependentFields, mocks, testClassInstance);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.configuration;
import org.mockito.ReturnValues;
import org.mockito.internal.configuration.InjectingAnnotationEngine;
import org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues;
import org.mockito.stubbing.Answer;
/**
* DefaultConfiguration of Mockito framework
* <p>
* Currently it doesn't have many configuration options but it will probably change if future.
* <p>
* See javadocs for {@link IMockitoConfiguration} on info how to configure Mockito
*/
@SuppressWarnings("deprecation")//suppressed until ReturnValues are removed
public class DefaultMockitoConfiguration implements IMockitoConfiguration {
/* (non-Javadoc)
* @see org.mockito.IMockitoConfiguration#getReturnValues()
*/
@Deprecated
public ReturnValues getReturnValues() {
throw new RuntimeException("\n" + "This method should not be used by the framework because it was deprecated"
+ "\n" + "Please report the failure to the Mockito mailing list");
}
public Answer<Object> getDefaultAnswer() {
return new ReturnsEmptyValues();
}
/* (non-Javadoc)
* @see org.mockito.IMockitoConfiguration#getAnnotationEngine()
*/
public AnnotationEngine getAnnotationEngine() {
return new InjectingAnnotationEngine();
}
/* (non-Javadoc)
* @see org.mockito.configuration.IMockitoConfiguration#cleansStackTrace()
*/
public boolean cleansStackTrace() {
return true;
}
/* (non-Javadoc)
* @see org.mockito.configuration.IMockitoConfiguration#enableClassCache()
*/
public boolean enableClassCache() {
return true;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.defaultanswers;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.Serializable;
import java.lang.reflect.Array;
/**
* It's likely this implementation will be used by default by every Mockito 2.0 mock.
* <p>
* Currently <b>used only</b> by {@link Mockito#RETURNS_SMART_NULLS}
* <p>
* Current version of Mockito mocks by deafult use {@link ReturnsEmptyValues}
* <ul>
* <li>
* Returns appropriate primitive for primitive-returning methods
* </li>
* <li>
* Returns consistent values for primitive wrapper classes (e.g. int-returning method retuns 0 <b>and</b> Integer-returning method returns 0, too)
* </li>
* <li>
* Returns empty collection for collection-returning methods (works for most commonly used collection types)
* </li>
* <li>
* Returns empty array for array-returning methods
* </li>
* <li>
* Returns "" for String-returning method
* </li>
* <li>
* Returns description of mock for toString() method
* </li>
* <li>
* Returns non-zero for Comparable#compareTo(T other) method (see issue 184)
* </li>
* <li>
* Returns null for everything else
* </li>
* </ul>
*/
public class ReturnsMoreEmptyValues implements Answer<Object>, Serializable {
private static final long serialVersionUID = -2816745041482698471L;
private final Answer<Object> delegate = new ReturnsEmptyValues();
/* (non-Javadoc)
* @see org.mockito.stubbing.Answer#answer(org.mockito.invocation.InvocationOnMock)
*/
public Object answer(InvocationOnMock invocation) throws Throwable {
Object
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> ret = delegate.answer(invocation);
if (ret != null) {
return ret;
}
Class<?> returnType = invocation.getMethod().getReturnType();
return returnValueFor(returnType);
}
Object returnValueFor(Class<?> type) {
if (type == String.class) {
return "";
} else if (type.isArray()) {
Class<?> componenetType = type.getComponentType();
return Array.newInstance(componenetType, 0);
}
return null;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.defaultanswers;
import java.io.Serializable;
import java.lang.reflect.Modifier;
import org.mockito.Mockito;
import org.mockito.exceptions.Reporter;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.invocation.Location;
import org.mockito.internal.util.ObjectMethodsGuru;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/**
* Optional Answer that can be used with
* {@link Mockito#mock(Class, Answer)}
* <p>
* This implementation can be helpful when working with legacy code. Unstubbed
* methods often return null. If your code uses the object returned by an
* unstubbed call you get a NullPointerException. This implementation of
* Answer returns SmartNulls instead of nulls.
* SmartNull gives nicer exception message than NPE because it points out the
* line where unstubbed method was called. You just click on the stack trace.
* <p>
* ReturnsSmartNulls first tries to return ordinary return values (see
* {@link ReturnsMoreEmptyValues}) then it tries to return SmartNull. If the
* return type is not mockable (e.g. final) then ordinary null is returned.
* <p>
* ReturnsSmartNulls will be probably the default return values strategy in
* Mockito 2.0
*/
public class ReturnsSmartNulls implements Answer<Object>, Serializable {
private static final long serialVersionUID = 7618312406617949441L;
private final Answer<Object> delegate = new ReturnsMoreEmptyValues();
public Object answer(final InvocationOnMock invocation) throws Throwable {
Object defaultReturnValue = delegate.answer(invocation);
if (defaultReturnValue != null) {
return defaultReturnValue;
}
Class<?> type = invocation.getMethod().getReturnType();
if (!type.isPrimitive() && !Modifier.isFinal(type.getModifiers())) {
final Location location = new LocationImpl();
return Mockito.mock(type, new ThrowsSmartNullPointer(invocation
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>, location));
}
return null;
}
private static class ThrowsSmartNullPointer implements Answer {
private final InvocationOnMock unstubbedInvocation;
private final Location location;
public ThrowsSmartNullPointer(InvocationOnMock unstubbedInvocation, Location location) {
this.unstubbedInvocation = unstubbedInvocation;
this.location = location;
}
public Object answer(InvocationOnMock currentInvocation) throws Throwable {
if (new ObjectMethodsGuru().isToString(currentInvocation.getMethod())) {
return "SmartNull returned by this unstubbed method call on a mock:\n" +
unstubbedInvocation.toString();
}
new Reporter().smartNullPointerException(unstubbedInvocation.toString(), location);
return null;
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection.scanner;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.internal.util.MockUtil;
import org.mockito.internal.util.reflection.FieldReader;
import java.lang.reflect.Field;
import java.util.Set;
import static org.mockito.internal.util.collections.Sets.newMockSafeHashSet;
/**
* Scan mocks, and prepare them if needed.
*/
public class MockScanner {
private final MockUtil mockUtil = new MockUtil();
private final Object instance;
private final Class<?> clazz;
/**
* Creates a MockScanner.
*
* @param instance The test instance
* @param clazz The class in the type hierarchy of this instance.
*/
public MockScanner(Object instance, Class<?> clazz) {
this.instance = instance;
this.clazz = clazz;
}
/**
* Add the scanned and prepared mock instance to the given collection.
*
* <p>
* The preparation of mocks consists only in defining a MockName if not already set.
* </p>
*
* @param mocks Set of mocks
*/
public void addPreparedMocks(Set<Object> mocks) {
mocks.addAll(scan());
}
/**
* Scan and prepare mocks for the given <code>testClassInstance</code> and <code>clazz</code> in the type hierarchy.
*
* @return A prepared set of mock
*/
private Set<Object> scan() {
Set<Object> mocks = newMockSafeHashSet();
for (Field field : clazz.getDeclaredFields()) {
// mock or spies only
FieldReader fieldReader = new FieldReader(instance, field);
Object mockInstance = preparedMock(fieldReader.read(), field);
if (mockInstance != null) {
mocks.add(mockInstance);
}
}
return mocks;
}
private Object preparedMock(Object instance, Field field) {
if (isAnnotatedByMockOrSpy(field)) {
return instance;
} else if (isMockOr
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>Spy(instance)) {
mockUtil.maybeRedefineMockName(instance, field.getName());
return instance;
}
return null;
}
private boolean isAnnotatedByMockOrSpy(Field field) {
return null != field.getAnnotation(Spy.class)
|| null != field.getAnnotation(Mock.class)
|| null != field.getAnnotation(MockitoAnnotations.Mock.class);
}
private boolean isMockOrSpy(Object instance) {
return mockUtil.isMock(instance)
|| mockUtil.isSpy(instance);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.cglib;
import org.mockito.cglib.proxy.Callback;
import org.mockito.cglib.proxy.Factory;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.InternalMockHandler;
import org.mockito.internal.creation.instance.InstantiatorProvider;
import org.mockito.invocation.MockHandler;
import org.mockito.mock.MockCreationSettings;
import org.mockito.plugins.MockMaker;
/**
* A MockMaker that uses cglib to generate mocks on a JVM.
*/
public class CglibMockMaker implements MockMaker {
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
InternalMockHandler mockitoHandler = cast(handler);
new AcrossJVMSerializationFeature().enableSerializationAcrossJVM(settings);
return new ClassImposterizer(new InstantiatorProvider().getInstantiator(settings)).imposterise(
new MethodInterceptorFilter(mockitoHandler, settings), settings.getTypeToMock(), settings.getExtraInterfaces());
}
private InternalMockHandler cast(MockHandler handler) {
if (!(handler instanceof InternalMockHandler)) {
throw new MockitoException("At the moment you cannot provide own implementations of MockHandler." +
"\nPlease see the javadocs for the MockMaker interface.");
}
return (InternalMockHandler) handler;
}
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
((Factory) mock).setCallback(0, new MethodInterceptorFilter(cast(newHandler), settings));
}
public MockHandler getHandler(Object mock) {
if (!(mock instanceof Factory)) {
return null;
}
Factory factory = (Factory) mock;
Callback callback = factory.getCallback(0);
if (!(callback instanceof MethodInterceptorFilter)) {
return null;
}
return ((MethodInterceptorFilter) callback).getHandler();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.defaultanswers;
import org.mockito.internal.util.MockUtil;
import org.mockito.internal.util.ObjectMethodsGuru;
import org.mockito.internal.util.Primitives;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.mock.MockName;
import org.mockito.stubbing.Answer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* Default answer of every Mockito mock.
* <ul>
* <li>
* Returns appropriate primitive for primitive-returning methods
* </li>
* <li>
* Returns consistent values for primitive wrapper classes (e.g. int-returning method retuns 0 <b>and</b> Integer-returning method returns 0, too)
* </li>
* <li>
* Returns empty collection for collection-returning methods (works for most commonly used collection types)
* </li>
* <li>
* Returns description of mock for toString() method
* </li>
* <li>
* Returns zero if references are equals otherwise non-zero for Comparable#compareTo(T other) method (see issue 184)
* </li>
* <li>
* Returns null for everything else
* </li>
* </ul>
*/
public class ReturnsEmptyValues implements Answer<Object>, Serializable {
private static final long serialVersionUID = 1998191268711234347L;
ObjectMethodsGuru methodsGuru = new ObjectMethodsGuru();
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> MockUtil mockUtil = new MockUtil();
/* (non-Javadoc)
* @see org.mockito.stubbing.Answer#answer(org.mockito.invocation.InvocationOnMock)
*/
public Object answer(InvocationOnMock invocation) {
if (methodsGuru.isToString(invocation.getMethod())) {
Object mock = invocation.getMock();
MockName name = mockUtil.getMockName(mock);
if (name.isDefault()) {
return "Mock for " + mockUtil.getMockSettings(mock).getTypeToMock().getSimpleName() + ", hashCode: " + mock.hashCode();
} else {
return name.toString();
}
} else if (methodsGuru.isCompareToMethod(invocation.getMethod())) {
//see issue 184.
//mocks by default should return 0 if references are the same, otherwise some other value because they are not the same. Hence we return 1 (anything but 0 is good).
//Only for compareTo() method by the Comparable interface
return invocation.getMock() == invocation.getArguments()[0] ? 0 : 1;
}
Class<?> returnType = invocation.getMethod().getReturnType();
return returnValueFor(returnType);
}
Object returnValueFor(Class<?> type) {
if (Primitives.isPrimitiveOrWrapper(type)) {
return Primitives.defaultValueForPrimitiveOrWrapper(type);
//new instances are used instead of Collections.emptyList(), etc.
//to avoid UnsupportedOperationException if code under test modifies returned collection
} else if (type == Collection.class) {
return new LinkedList<Object>();
} else if (type == Set.class) {
return new HashSet<Object>();
} else if (type == HashSet.class) {
return new HashSet<Object>();
} else if (type == SortedSet.class) {
return new TreeSet<Object>();
} else if (type == TreeSet.class) {
return new TreeSet<Object>();
} else if (type == LinkedHashSet.class) {
return new LinkedHashSet<Object>();
} else if (type == List.class) {
return new LinkedList<Object>();
} else if (type == LinkedList.class) {
return new LinkedList<Object>();
} else if (type == ArrayList.class) {
return new ArrayList<Object>();
} else if (
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>type == Map.class) {
return new HashMap<Object, Object>();
} else if (type == HashMap.class) {
return new HashMap<Object, Object>();
} else if (type == SortedMap.class) {
return new TreeMap<Object, Object>();
} else if (type == TreeMap.class) {
return new TreeMap<Object, Object>();
} else if (type == LinkedHashMap.class) {
return new LinkedHashMap<Object, Object>();
}
// TODO return empty Iterable ; see issue 175
//Let's not care about the rest of collections.
return null;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation;
import org.hamcrest.Matcher;
import org.mockito.internal.matchers.CapturesArguments;
import org.mockito.internal.matchers.MatcherDecorator;
import org.mockito.internal.matchers.VarargMatcher;
import org.mockito.internal.reporting.PrintSettings;
import org.mockito.invocation.DescribedInvocation;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.Location;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@SuppressWarnings("unchecked")
public class InvocationMatcher implements DescribedInvocation, CapturesArgumensFromInvocation, Serializable {
private static final long serialVersionUID = -3047126096857467610L;
private final Invocation invocation;
private final List<Matcher> matchers;
public InvocationMatcher(Invocation invocation, List<Matcher> matchers) {
this.invocation = invocation;
if (matchers.isEmpty()) {
this.matchers = ArgumentsProcessor.argumentsToMatchers(invocation.getArguments());
} else {
this.matchers = matchers;
}
}
public InvocationMatcher(Invocation invocation) {
this(invocation, Collections.<Matcher>emptyList());
}
public Method getMethod() {
return invocation.getMethod();
}
public Invocation getInvocation() {
return this.invocation;
}
public List<Matcher> getMatchers() {
return this.matchers;
}
public String toString() {
return new PrintSettings().print(matchers, invocation);
}
public boolean matches(Invocation actual) {
return invocation.getMock().equals(actual.getMock())
&& hasSameMethod(actual)
&& new ArgumentsComparator().argumentsMatch(this, actual);
}
private boolean safelyArgumentsMatch(Object[] actualArgs) {
try {
return new ArgumentsComparator().argumentsMatch(this, actualArgs);
} catch (Throwable t) {
return false;
}
}
/**
* similar means the
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> same method name, same mock, unverified
* and: if arguments are the same cannot be overloaded
*/
public boolean hasSimilarMethod(Invocation candidate) {
String wantedMethodName = getMethod().getName();
String currentMethodName = candidate.getMethod().getName();
final boolean methodNameEquals = wantedMethodName.equals(currentMethodName);
final boolean isUnverified = !candidate.isVerified();
final boolean mockIsTheSame = getInvocation().getMock() == candidate.getMock();
final boolean methodEquals = hasSameMethod(candidate);
if (!methodNameEquals || !isUnverified || !mockIsTheSame) {
return false;
}
final boolean overloadedButSameArgs = !methodEquals && safelyArgumentsMatch(candidate.getArguments());
return !overloadedButSameArgs;
}
public boolean hasSameMethod(Invocation candidate) {
//not using method.equals() for 1 good reason:
//sometimes java generates forwarding methods when generics are in play see JavaGenericsForwardingMethodsTest
Method m1 = invocation.getMethod();
Method m2 = candidate.getMethod();
if (m1.getName() != null && m1.getName().equals(m2.getName())) {
/* Avoid unnecessary cloning */
Class[] params1 = m1.getParameterTypes();
Class[] params2 = m2.getParameterTypes();
if (params1.length == params2.length) {
for (int i = 0; i < params1.length; i++) {
if (params1[i] != params2[i])
return false;
}
return true;
}
}
return false;
}
public Location getLocation() {
return invocation.getLocation();
}
public void captureArgumentsFrom(Invocation invocation) {
for (int position = 0; position < matchers.size(); position++) {
Matcher m = matchers.get(position);
if (m instanceof CapturesArguments && invocation.getRawArguments().length > position) {
//TODO SF - this whole lot can be moved captureFrom implementation
if(isVariableArgument(invocation, position) && isVarargMatcher(m)) {
Object array = invocation.getRawArguments()[position];
for (int i = 0; i < Array.getLength(array); i++) {
((Captures
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.answers;
import java.io.Serializable;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
public class Returns implements Answer<Object>, Serializable {
private static final long serialVersionUID = -6245608253574215396L;
private final Object value;
public Returns(Object value) {
this.value = value;
}
public Object answer(InvocationOnMock invocation) throws Throwable {
return value;
}
public String printReturnType() {
return value.getClass().getSimpleName();
}
public Class<?> getReturnType() {
return value.getClass();
}
public boolean returnsNull() {
return value == null;
}
@Override
public String toString() {
return "Returns: " + value;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation.realmethod;
public interface RealMethod {
Object invoke(Object target, Object[] arguments) throws Throwable;
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> the assertor!
protected static <T> void assertThat(T o, Assertor<T> a) {
a.assertValue(o);
}
protected static <T> void assertThat(T actual, Matcher<T> m) {
org.junit.Assert.assertThat(actual, m);
}
protected static <T> void assertThat(String message, T actual, Matcher<T> m) {
org.junit.Assert.assertThat(message, actual, m);
}
public static <T> Assertor<String> endsWith(final String substring) {
return new Assertor<String>() {
public void assertValue(String value) {
assertTrue("This substring: \n" + substring +
"\nshould be at the end of:\n" + value
, value.endsWith(substring));
}
};
}
public static void assertNotEquals(Object expected, Object got) {
assertFalse(expected.equals(got));
}
public static void assertContains(String sub, String string) {
assertTrue("\n" +
"This substring:[" +
sub +
"]\n" +
"should be inside of:[" +
string +
"]\n"
, string.contains(sub));
}
public static void assertContainsIgnoringCase(String sub, String string) {
assertTrue("\n" +
"This substring:" +
sub +
"\n" +
"should be inside of:" +
string +
"\n"
, containsIgnoringCase(string, sub));
}
private static boolean containsIgnoringCase(String string, String sub) {
int subLength = sub.length();
if (string.length() < subLength) {
return false;
}
int i = 0;
while(i+subLength <= string.length()) {
boolean temp = string.substring(i, i+subLength).equalsIgnoreCase(sub);
if (temp) {
return true;
}
i++;
}
return false;
}
public static void assertNotContains(String sub, String string) {
assertFalse("\n" +
"This substring:" +
sub +
"\n" +
"should NOT be inside of:" +
string +
"\n"
, string.contains(sub));
}
protected static Invocation
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> invocationOf(Class<?> type, String methodName, Object ... args) throws NoSuchMethodException {
Class[] types = new Class[args.length];
for (int i = 0; i < args.length; i++) {
types[i] = args[i].getClass();
}
return new InvocationImpl(mock(type), new SerializableMethod(type.getMethod(methodName,
types)), args, 1, null);
}
protected static Invocation invocationOf(Class<?> type, String methodName, RealMethod realMethod) throws NoSuchMethodException {
return new InvocationImpl(new Object(), new SerializableMethod(type.getMethod(methodName,
new Class[0])), new Object[0], 1, realMethod);
}
protected static String describe(SelfDescribing m) {
return StringDescription.toString(m);
}
protected boolean isMock(Object o) {
return new MockUtil().isMock(o);
}
protected void assertContainsType(final Collection<?> list, final Class<?> clazz) {
Assertions.assertThat(list).satisfies(new Condition<Collection<?>>() {
@Override
public boolean matches(Collection<?> objects) {
for (Object object : objects) {
if (clazz.isAssignableFrom(object.getClass())) {
return true;
}
}
return false;
}
});
}
protected String getStackTrace(Throwable e) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(out));
try {
out.close();
} catch (IOException ex) {}
return out.toString();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>p>
* See javadoc {@link ReturnValues} for info why this method was deprecated
* <p>
* Allows configuring the default return values of unstubbed invocations
* <p>
* See javadoc for {@link IMockitoConfiguration}
*/
@Deprecated
ReturnValues getReturnValues();
/**
* Allows configuring the default answers of unstubbed invocations
* <p>
* See javadoc for {@link IMockitoConfiguration}
*/
Answer<Object> getDefaultAnswer();
/**
* Configures annotations for mocks
* <p>
* See javadoc for {@link IMockitoConfiguration}
*/
AnnotationEngine getAnnotationEngine();
/**
* This should be turned on unless you're a Mockito developer and you wish
* to have verbose (read: messy) stack traces that only few understand (eg:
* Mockito developers)
* <p>
* See javadoc for {@link IMockitoConfiguration}
*
* @return if Mockito should clean stack traces
*/
boolean cleansStackTrace();
/**
* Allow objenesis to cache classes. If you're in an environment where classes
* are dynamically reloaded, you can disable this to avoid classcast exceptions.
*/
boolean enableClassCache();
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.configuration;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import org.mockito.Mockito;
import org.mockito.internal.configuration.InjectingAnnotationEngine;
import org.mockito.stubbing.Answer;
import org.mockitousage.configuration.SmartMock;
public class MockitoConfiguration extends DefaultMockitoConfiguration implements IMockitoConfiguration {
private Answer<Object> overriddenDefaultAnswer = null;
private boolean cleansStackTrace;
private AnnotationEngine overriddenEngine;
private boolean enableClassCache = true;
//for testing purposes, allow to override the configuration
public void overrideDefaultAnswer(Answer<Object> defaultAnswer) {
this.overriddenDefaultAnswer = defaultAnswer;
}
//for testing purposes, allow to override the configuration
public void overrideCleansStackTrace(boolean cleansStackTrace) {
this.cleansStackTrace = cleansStackTrace;
}
//for testing purposes, allow to override the annotation engine
public void overrideAnnotationEngine(AnnotationEngine engine) {
this.overriddenEngine = engine;
}
//for testing purposes, allow to override the annotation engine
public void overrideEnableClassCache(boolean enableClassCache) {
this.enableClassCache = enableClassCache;
}
@Override
public Answer<Object> getDefaultAnswer() {
if (overriddenDefaultAnswer == null) {
return super.getDefaultAnswer();
} else {
return overriddenDefaultAnswer;
}
}
@Override
public AnnotationEngine getAnnotationEngine() {
if (this.overriddenEngine != null) {
return this.overriddenEngine;
}
return new InjectingAnnotationEngine() {
@Override
public Object createMockFor(Annotation annotation, Field field) {
if (annotation instanceof SmartMock) {
return Mockito.mock(field.getType(), Mockito.RETURNS_SMART_NULLS);
} else {
return super.createMockFor(annotation, field);
}
}
};
}
@Override
public boolean cleansStackTrace() {
return cleansStackTrace;
}
@Override
public boolean enableClassCache() {
return enableClassCache;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util.reflection;
import java.lang.reflect.AccessibleObject;
public class AccessibilityChanger {
private Boolean wasAccessible = null;
/**
* safely disables access
*/
public void safelyDisableAccess(AccessibleObject accessibleObject) {
assert wasAccessible != null : "accessibility info shall not be null";
try {
accessibleObject.setAccessible(wasAccessible);
} catch (Throwable t) {
//ignore
}
}
/**
* changes the accessibleObject accessibility and returns true if accessibility was changed
*/
public void enableAccess(AccessibleObject accessibleObject) {
wasAccessible = accessibleObject.isAccessible();
accessibleObject.setAccessible(true);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import org.mockito.ReturnValues;
import org.mockito.configuration.AnnotationEngine;
import org.mockito.configuration.DefaultMockitoConfiguration;
import org.mockito.configuration.IMockitoConfiguration;
import org.mockito.stubbing.Answer;
import java.io.Serializable;
/**
* Thread-safe wrapper on user-defined org.mockito.configuration.MockitoConfiguration implementation
*/
@SuppressWarnings("deprecation")//suppressed until ReturnValues are removed
public class GlobalConfiguration implements IMockitoConfiguration, Serializable {
private static final long serialVersionUID = -2860353062105505938L;
private static final ThreadLocal<IMockitoConfiguration> GLOBAL_CONFIGURATION = new ThreadLocal<IMockitoConfiguration>();
//back door for testing
IMockitoConfiguration getIt() {
return GLOBAL_CONFIGURATION.get();
}
public GlobalConfiguration() {
//Configuration should be loaded only once but I cannot really test it
if (GLOBAL_CONFIGURATION.get() == null) {
GLOBAL_CONFIGURATION.set(createConfig());
}
}
private IMockitoConfiguration createConfig() {
IMockitoConfiguration defaultConfiguration = new DefaultMockitoConfiguration();
IMockitoConfiguration config = new ClassPathLoader().loadConfiguration();
if (config != null) {
return config;
} else {
return defaultConfiguration;
}
}
public static void validate() {
new GlobalConfiguration();
}
public ReturnValues getReturnValues() {
return GLOBAL_CONFIGURATION.get().getReturnValues();
}
public AnnotationEngine getAnnotationEngine() {
return GLOBAL_CONFIGURATION.get().getAnnotationEngine();
}
public boolean cleansStackTrace() {
return GLOBAL_CONFIGURATION.get().cleansStackTrace();
}
public boolean enableClassCache() {
return GLOBAL_CONFIGURATION.get().enableClassCache();
}
public Answer<Object> getDefaultAnswer() {
return GLOBAL_CONFIGURATION.get().getDefaultAnswer();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> locationsOf(recordedMatchers),
"",
"This exception may occur if matchers are combined with raw values:",
" //incorrect:",
" someMethod(anyObject(), \"raw String\");",
"When using matchers, all arguments have to be provided by matchers.",
"For example:",
" //correct:",
" someMethod(anyObject(), eq(\"String by matcher\"));",
"",
"For more info see javadoc for Matchers class.",
""
));
}
public void incorrectUseOfAdditionalMatchers(String additionalMatcherName, int expectedSubMatchersCount, Collection<LocalizedMatcher> matcherStack) {
throw new InvalidUseOfMatchersException(join(
"Invalid use of argument matchers inside additional matcher " + additionalMatcherName + " !",
new LocationImpl(),
"",
expectedSubMatchersCount + " sub matchers expected, " + matcherStack.size() + " recorded:",
locationsOf(matcherStack),
"",
"This exception may occur if matchers are combined with raw values:",
" //incorrect:",
" someMethod(AdditionalMatchers.and(isNotNull(), \"raw String\");",
"When using matchers, all arguments have to be provided by matchers.",
"For example:",
" //correct:",
" someMethod(AdditionalMatchers.and(isNotNull(), eq(\"raw String\"));",
"",
"For more info see javadoc for Matchers and AdditionalMatchers classes.",
""
));
}
public void stubPassedToVerify() {
throw new CannotVerifyStubOnlyMock(join(
"Argument passed to verify() is a stubOnly() mock, not a full blown mock!",
"If you intend to verify invocations on a mock, don't use stubOnly() in its MockSettings."
));
}
public void reportNoSubMatchersFound(String additionalMatcherName) {
throw new InvalidUseOfMatchersException(join(
"No matchers found for additional matcher " + additionalMatcherName,
new LocationImpl(),
""
));
}
private Object locationsOf(Collection<LocalizedMatcher> matchers) {
List<String> description = new ArrayList<String>();
for (LocalizedMatcher matcher : matchers)
description.add(matcher.getLocation().toString());
return join(description.toArray());
}
public void argumentsAreDifferent(String wanted,
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>(),
"because this method call was *not* stubbed correctly:",
location,
invocation,
""
));
}
public void noArgumentValueWasCaptured() {
throw new MockitoException(join(
"No argument value was captured!",
"You might have forgotten to use argument.capture() in verify()...",
"...or you used capture() in stubbing but stubbed method was not called.",
"Be aware that it is recommended to use capture() only with verify()",
"",
"Examples of correct argument capturing:",
" ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);",
" verify(mock).doSomething(argument.capture());",
" assertEquals(\"John\", argument.getValue().getName());",
""
));
}
public void extraInterfacesDoesNotAcceptNullParameters() {
throw new MockitoException(join(
"extraInterfaces() does not accept null parameters."
));
}
public void extraInterfacesAcceptsOnlyInterfaces(Class<?> wrongType) {
throw new MockitoException(join(
"extraInterfaces() accepts only interfaces.",
"You passed following type: " + wrongType.getSimpleName() + " which is not an interface."
));
}
public void extraInterfacesCannotContainMockedType(Class<?> wrongType) {
throw new MockitoException(join(
"extraInterfaces() does not accept the same type as the mocked type.",
"You mocked following type: " + wrongType.getSimpleName(),
"and you passed the same very interface to the extraInterfaces()"
));
}
public void extraInterfacesRequiresAtLeastOneInterface() {
throw new MockitoException(join(
"extraInterfaces() requires at least one interface."
));
}
public void mockedTypeIsInconsistentWithSpiedInstanceType(Class<?> mockedType, Object spiedInstance) {
throw new MockitoException(join(
"Mocked type must be the same as the type of your spied instance.",
"Mocked type must be: " + spiedInstance.getClass().getSimpleName() + ", but is: " + mockedType.getSimpleName(),
" //correct spying:",
" spy = mock( ->ArrayList.class<- , withSettings().spiedInstance( ->new ArrayList()<- );",
" //incorrect - types don't match:",
" spy = mock( ->List.class
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> fieldName, Exception details) {
throw new MockitoException(join("Cannot instantiate @InjectMocks field named '" + fieldName + "'.",
"You haven't provided the instance at field declaration so I tried to construct the instance.",
"However, I failed because: " + details.getMessage(),
"Examples of correct usage of @InjectMocks:",
" @InjectMocks Service service = new Service();",
" @InjectMocks Service service;",
" //also, don't forget about MockitoAnnotations.initMocks();",
" //and... don't forget about some @Mocks for injection :)",
""), details);
}
public void atMostAndNeverShouldNotBeUsedWithTimeout() {
throw new FriendlyReminderException(join("",
"Don't panic! I'm just a friendly reminder!",
"timeout() should not be used with atMost() or never() because...",
"...it does not make much sense - the test would have passed immediately in concurency",
"We kept this method only to avoid compilation errors when upgrading Mockito.",
"In future release we will remove timeout(x).atMost(y) from the API.",
"If you want to find out more please refer to issue 235",
""));
}
public void fieldInitialisationThrewException(Field field, Throwable details) {
throw new MockitoException(join(
"Cannot instantiate @InjectMocks field named '" + field.getName() + "' of type '" + field.getType() + "'.",
"You haven't provided the instance at field declaration so I tried to construct the instance.",
"However the constructor or the initialization block threw an exception : " + details.getMessage(),
""), details);
}
public void invocationListenerDoesNotAcceptNullParameters() {
throw new MockitoException("invocationListeners() does not accept null parameters");
}
public void invocationListenersRequiresAtLeastOneListener() {
throw new MockitoException("invocationListeners() requires at least one listener");
}
public void invocationListenerThrewException(InvocationListener listener, Throwable listenerThrowable) {
throw new MockitoException(StringJoiner.join(
"The invocation listener with type " + listener.getClass().getName(),
"threw an exception : " + listenerThrowable.getClass().getName() + listenerThrowable.getMessage()), listenerThrowable);
}
public void cannotInjectDependency(Field field, Object matchingMock, Exception
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> details) {
throw new MockitoException(join(
"Mockito couldn't inject mock dependency '" + new MockUtil().getMockName(matchingMock) + "' on field ",
"'" + field + "'",
"whose type '" + field.getDeclaringClass().getCanonicalName() + "' was annotated by @InjectMocks in your test.",
"Also I failed because: " + details.getCause().getMessage(),
""
), details);
}
public void mockedTypeIsInconsistentWithDelegatedInstanceType(Class mockedType, Object delegatedInstance) {
throw new MockitoException(join(
"Mocked type must be the same as the type of your delegated instance.",
"Mocked type must be: " + delegatedInstance.getClass().getSimpleName() + ", but is: " + mockedType.getSimpleName(),
" //correct delegate:",
" spy = mock( ->List.class<- , withSettings().delegatedInstance( ->new ArrayList()<- );",
" //incorrect - types don't match:",
" spy = mock( ->List.class<- , withSettings().delegatedInstance( ->new HashSet()<- );"
));
}
public void spyAndDelegateAreMutuallyExclusive() {
throw new MockitoException(join(
"Settings should not define a spy instance and a delegated instance at the same time."
)) ;
}
public void invalidArgumentRangeAtIdentityAnswerCreationTime() {
throw new MockitoException(join("Invalid argument index.",
"The index need to be a positive number that indicates the position of the argument to return.",
"However it is possible to use the -1 value to indicates that the last argument should be",
"returned."));
}
public int invalidArgumentPositionRangeAtInvocationTime(InvocationOnMock invocation, boolean willReturnLastParameter, int argumentIndex) {
throw new MockitoException(
join("Invalid argument index for the current invocation of method : ",
" -> " + new MockUtil().getMockName(invocation.getMock()) + "." + invocation.getMethod().getName() + "()",
"",
(willReturnLastParameter ?
"Last parameter wanted" :
"Wanted parameter at position " + argumentIndex) + " but " + possibleArgumentTypesOf(invocation),
"The index need to be a positive number that indicates a valid position of the argument in the invocation.",
"However it is possible to
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>
}
public void defaultAnswerDoesNotAcceptNullParameter() {
throw new MockitoException("defaultAnswer() does not accept null parameter");
}
public void serializableWontWorkForObjectsThatDontImplementSerializable(Class classToMock) {
throw new MockitoException(join(
"You are using the setting 'withSettings().serializable()' however the type you are trying to mock '" + classToMock.getSimpleName() + "'",
"do not implement Serializable AND do not have a no-arg constructor.",
"This combination is requested, otherwise you will get an 'java.io.InvalidClassException' when the mock will be serialized",
"",
"Also note that as requested by the Java serialization specification, the whole hierarchy need to implements Serializable,",
"i.e. the top-most superclass has to implements Serializable.",
""
));
}
public void delegatedMethodHasWrongReturnType(Method mockMethod, Method delegateMethod, Object mock, Object delegate) {
throw new MockitoException(join(
"Methods called on delegated instance must have compatible return types with the mock.",
"When calling: " + mockMethod + " on mock: " + new MockUtil().getMockName(mock),
"return type should be: " + mockMethod.getReturnType().getSimpleName() + ", but was: " + delegateMethod.getReturnType().getSimpleName(),
"Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods",
"(delegate instance had type: " + delegate.getClass().getSimpleName() + ")"
));
}
public void delegatedMethodDoesNotExistOnDelegate(Method mockMethod, Object mock, Object delegate) {
throw new MockitoException(join(
"Methods called on mock must exist in delegated instance.",
"When calling: " + mockMethod + " on mock: " + new MockUtil().getMockName(mock),
"no such method was found.",
"Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods",
"(delegate instance had type: " + delegate.getClass().getSimpleName() + ")"
));
}
public void usingConstructorWithFancySerializable(SerializableMode mode) {
throw new MockitoException("Mocks instantiated with constructor cannot be combined with " + mode + " serialization mode.");
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>> line with
* the qualified name <code>org.awesome.mockito.AwesomeMockMaker</code>.</li>
* </ol>
* </li>
* </ul>
* </p>
*/
public class ClassPathLoader {
public static final String MOCKITO_CONFIGURATION_CLASS_NAME = "org.mockito.configuration.MockitoConfiguration";
/**
* @return configuration loaded from classpath or null
*/
@SuppressWarnings({"unchecked"})
public IMockitoConfiguration loadConfiguration() {
//Trying to get config from classpath
Class configClass;
try {
configClass = (Class) Class.forName(MOCKITO_CONFIGURATION_CLASS_NAME);
} catch (ClassNotFoundException e) {
//that's ok, it means there is no global config, using default one.
return null;
}
try {
return (IMockitoConfiguration) configClass.newInstance();
} catch (ClassCastException e) {
throw new MockitoConfigurationException("MockitoConfiguration class must implement " + IMockitoConfiguration.class.getName() + " interface.", e);
} catch (Exception e) {
throw new MockitoConfigurationException("Unable to instantiate " + MOCKITO_CONFIGURATION_CLASS_NAME +" class. Does it have a safe, no-arg constructor?", e);
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>
* </ul>
* <li>else don't fail, user will then provide dependencies
* </ul>
* </ul>
* </p>
*
* <p>
* <u>Note:</u> If the field needing injection is not initialized, the strategy tries
* to create one using a no-arg constructor of the field type.
* </p>
*/
public class PropertyAndSetterInjection extends MockInjectionStrategy {
private final MockCandidateFilter mockCandidateFilter = new TypeBasedCandidateFilter(new NameBasedCandidateFilter(new FinalMockCandidateFilter()));
private final Comparator<Field> superTypesLast = new FieldTypeAndNameComparator();
private final ListUtil.Filter<Field> notFinalOrStatic = new ListUtil.Filter<Field>() {
public boolean isOut(Field object) {
return Modifier.isFinal(object.getModifiers()) || Modifier.isStatic(object.getModifiers());
}
};
public boolean processInjection(Field injectMocksField, Object injectMocksFieldOwner, Set<Object> mockCandidates) {
// Set<Object> mocksToBeInjected = new HashSet<Object>(mockCandidates);
FieldInitializationReport report = initializeInjectMocksField(injectMocksField, injectMocksFieldOwner);
// for each field in the class hierarchy
boolean injectionOccurred = false;
Class<?> fieldClass = report.fieldClass();
Object fieldInstanceNeedingInjection = report.fieldInstance();
while (fieldClass != Object.class) {
injectionOccurred |= injectMockCandidates(fieldClass, newMockSafeHashSet(mockCandidates), fieldInstanceNeedingInjection);
fieldClass = fieldClass.getSuperclass();
}
return injectionOccurred;
}
private FieldInitializationReport initializeInjectMocksField(Field field, Object fieldOwner) {
FieldInitializationReport report = null;
try {
report = new FieldInitializer(fieldOwner, field).initialize();
} catch (MockitoException e) {
if(e.getCause() instanceof InvocationTargetException) {
Throwable realCause = e.getCause().getCause();
new Reporter().fieldInitialisationThrewException(field, realCause);
}
new Reporter().cannotInitializeForInjectMocksAnnotation(field.getName(), e);
}
return report; // never null
}
private boolean injectMockCandidates(Class<?> awaitingInjectionClazz,
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> Set<Object> mocks, Object instance) {
boolean injectionOccurred = false;
List<Field> orderedInstanceFields = orderedInstanceFieldsFrom(awaitingInjectionClazz);
// pass 1
injectionOccurred |= injectMockCandidatesOnFields(mocks, instance, injectionOccurred, orderedInstanceFields);
// pass 2
injectionOccurred |= injectMockCandidatesOnFields(mocks, instance, injectionOccurred, orderedInstanceFields);
return injectionOccurred;
}
private boolean injectMockCandidatesOnFields(Set<Object> mocks, Object instance, boolean injectionOccurred, List<Field> orderedInstanceFields) {
for (Iterator<Field> it = orderedInstanceFields.iterator(); it.hasNext(); ) {
Field field = it.next();
Object injected = mockCandidateFilter.filterCandidate(mocks, field, instance).thenInject();
if (injected != null) {
injectionOccurred |= true;
mocks.remove(injected);
it.remove();
}
}
return injectionOccurred;
}
private List<Field> orderedInstanceFieldsFrom(Class<?> awaitingInjectionClazz) {
List<Field> declaredFields = Arrays.asList(awaitingInjectionClazz.getDeclaredFields());
declaredFields = ListUtil.filter(declaredFields, notFinalOrStatic);
Collections.sort(declaredFields, superTypesLast);
return declaredFields;
}
static class FieldTypeAndNameComparator implements Comparator<Field> {
public int compare(Field field1, Field field2) {
Class<?> field1Type = field1.getType();
Class<?> field2Type = field2.getType();
// if same type, compares on field name
if (field1Type == field2Type) {
return field1.getName().compareTo(field2.getName());
}
if(field1Type.isAssignableFrom(field2Type)) {
return 1;
}
if(field2Type.isAssignableFrom(field1Type)) {
return -1;
}
return 0;
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
/**
* Pre-made preconditions
*/
public class Checks {
public static <T> T checkNotNull(T value, String checkedValue) {
if(value == null) {
throw new NullPointerException(checkedValue + " should not be null");
}
return value;
}
public static <T extends Iterable> T checkItemsNotNull(T iterable, String checkedIterable) {
checkNotNull(iterable, checkedIterable);
for (Object item : iterable) {
checkNotNull(item, "item in " + checkedIterable);
}
return iterable;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import org.mockito.internal.configuration.injection.MockInjection;
import java.lang.reflect.Field;
import java.util.Set;
/**
* Inject mock/spies dependencies for fields annotated with @InjectMocks
* <p/>
* See {@link org.mockito.MockitoAnnotations}
*/
public class DefaultInjectionEngine {
public void injectMocksOnFields(Set<Field> needingInjection, Set<Object> mocks, Object testClassInstance) {
MockInjection.onFields(needingInjection, testClassInstance)
.withMocks(mocks)
.tryConstructorInjection()
.tryPropertyOrFieldInjection()
.handleSpyAnnotation()
.apply();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>.cglib.MethodInterceptorFilter}</strong></p>
*
* TODO use a proper way to add the interface
* TODO offer a way to disable completely this behavior, or maybe enable this behavior only with a specific setting
* TODO check the class is mockable in the deserialization side
*
* @see org.mockito.internal.creation.cglib.CglibMockMaker
* @see org.mockito.internal.creation.cglib.MethodInterceptorFilter
* @author Brice Dutheil
* @since 1.10.0
*/
@Incubating
class AcrossJVMSerializationFeature implements Serializable {
private static final long serialVersionUID = 7411152578314420778L;
private static final String MOCKITO_PROXY_MARKER = "MockitoProxyMarker";
private boolean instanceLocalCurrentlySerializingFlag = false;
private final Lock mutex = new ReentrantLock();
public boolean isWriteReplace(Method method) {
return method.getReturnType() == Object.class
&& method.getParameterTypes().length == 0
&& method.getName().equals("writeReplace");
}
/**
* Custom implementation of the <code>writeReplace</code> method for serialization.
*
* Here's how it's working and why :
* <ol>
* <li>
* <p>When first entering in this method, it's because some is serializing the mock, with some code like :
* <pre class="code"><code class="java">
* objectOutputStream.writeObject(mock);
* </code></pre>
* So, {@link ObjectOutputStream} will track the <code>writeReplace</code> method in the instance and
* execute it, which is wanted to replace the mock by another type that will encapsulate the actual mock.
* At this point, the code will return an
* {@link AcrossJVMSerializationFeature.AcrossJVMMockSerializationProxy}.</p>
* </li>
* <li>
* <p>Now, in the constructor
* {@link AcrossJVMSerializationFeature.AcrossJVMMockSerializationProxy#AcrossJVMMockSerializationProxy(Object)}
* the mock is being serialized in a custom way (using
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> * {@link AcrossJVMSerializationFeature.MockitoMockObjectOutputStream}) to a
* byte array. So basically it means the code is performing double nested serialization of the passed
* <code>mockitoMock</code>.</p>
*
* <p>However the <code>ObjectOutputStream</code> will still detect the custom
* <code>writeReplace</code> and execute it.
* <em>(For that matter disabling replacement via {@link ObjectOutputStream#enableReplaceObject(boolean)}
* doesn't disable the <code>writeReplace</code> call, but just just toggle replacement in the
* written stream, <strong><code>writeReplace</code> is always called by
* <code>ObjectOutputStream</code></strong>.)</em></p>
*
* <p>In order to avoid this recursion, obviously leading to a {@link StackOverflowError}, this method is using
* a flag that marks the mock as already being replaced, and then shouldn't replace itself again.
* <strong>This flag is local to this class</strong>, which means the flag of this class unfortunately needs
* to be protected against concurrent access, hence the reentrant lock.</p>
* </li>
* </ol>
*
*
* @param mockitoMock The Mockito mock to be serialized.
* @return A wrapper ({@link AcrossJVMMockSerializationProxy}) to be serialized by the calling ObjectOutputStream.
* @throws ObjectStreamException
*/
public Object writeReplace(Object mockitoMock) throws ObjectStreamException {
try {
// reentrant lock for critical section. could it be improved ?
mutex.lock();
// mark started flag // per thread, not per instance
// temporary loosy hack to avoid stackoverflow
if(mockIsCurrentlyBeingReplaced()) {
return mockitoMock;
}
mockReplacementStarted();
return new AcrossJVMMockSerializationProxy(mockitoMock);
} catch (IOException ioe) {
MockUtil mockUtil = new MockUtil();
MockName mockName = mockUtil.getMockName(mockitoMock);
String mockedType = mockUtil.getMockSettings(mockitoMock).getTypeToMock().getCanonicalName();
throw new MockitoSerializationIssue(join(
"The mock '" + mockName + "' of type '" + mocked
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>Type + "'",
"The Java Standard Serialization reported an '" + ioe.getClass().getSimpleName() + "' saying :",
" " + ioe.getMessage()
), ioe);
} finally {
// unmark
mockReplacementCompleted();
mutex.unlock();
}
}
private void mockReplacementCompleted() {
instanceLocalCurrentlySerializingFlag = false;
}
private void mockReplacementStarted() {
instanceLocalCurrentlySerializingFlag = true;
}
private boolean mockIsCurrentlyBeingReplaced() {
return instanceLocalCurrentlySerializingFlag;
}
/**
* Enable serialization serialization that will work across classloaders / and JVM.
*
* <p>Only enable if settings says the mock should be serializable. In this case add the
* {@link AcrossJVMMockitoMockSerializable} to the extra interface list.</p>
*
* @param settings Mock creation settings.
* @param <T> Type param to not be bothered by the generics
*/
public <T> void enableSerializationAcrossJVM(MockCreationSettings<T> settings) {
if (settings.getSerializableMode() == SerializableMode.ACROSS_CLASSLOADERS) {
// havin faith that this set is modifiable
// TODO use a proper way to add the interface
settings.getExtraInterfaces().add(AcrossJVMMockitoMockSerializable.class);
}
}
/**
* This is the serialization proxy that will encapsulate the real mock data as a byte array.
*
* <p>When called in the constructor it will serialize the mock in a byte array using a
* custom {@link AcrossJVMSerializationFeature.MockitoMockObjectOutputStream} that
* will annotate the mock class in the stream.
* Other information are used in this class in order to facilitate deserialization.
* </p>
*
* <p>Deserialization of the mock will be performed by the {@link #readResolve()} method via
* the custom {@link MockitoMockObjectInputStream} that will be in charge of creating the mock class.</p>
*/
public static class AcrossJVMMockSerializationProxy implements Serializable {
private static final long serialVersionUID = -7600267929109286514L;
private final byte[] serializedMock;
private final Class typeToMock
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>;
private final Set<Class> extraInterfaces;
/**
* Creates the wrapper that be used in the serialization stream.
*
* <p>Immediately serializes the Mockito mock using specifically crafted
* {@link AcrossJVMSerializationFeature.MockitoMockObjectOutputStream},
* in a byte array.</p>
*
* @param mockitoMock The Mockito mock to serialize.
* @throws IOException
*/
public AcrossJVMMockSerializationProxy(Object mockitoMock) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new MockitoMockObjectOutputStream(out);
objectOutputStream.writeObject(mockitoMock);
objectOutputStream.close();
out.close();
MockCreationSettings mockSettings = new MockUtil().getMockSettings(mockitoMock);
this.serializedMock = out.toByteArray();
this.typeToMock = mockSettings.getTypeToMock();
this.extraInterfaces = mockSettings.getExtraInterfaces();
}
/**
* Resolves the proxy to a new deserialized instance of the Mockito mock.
*
* <p>Uses the custom crafted {@link MockitoMockObjectInputStream} to deserialize the mock.</p>
*
* @return A deserialized instance of the Mockito mock.
* @throws ObjectStreamException
*/
private Object readResolve() throws ObjectStreamException {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(serializedMock);
ObjectInputStream objectInputStream = new MockitoMockObjectInputStream(bis, typeToMock, extraInterfaces);
Object deserializedMock = objectInputStream.readObject();
bis.close();
objectInputStream.close();
return deserializedMock;
} catch (IOException ioe) {
throw new MockitoSerializationIssue(join(
"Mockito mock cannot be deserialized to a mock of '" + typeToMock.getCanonicalName() + "'. The error was :",
" " + ioe.getMessage(),
"If you are unsure what is the reason of this exception, feel free to contact us on the mailing list."
), ioe);
} catch (ClassNotFoundException cce) {
throw new MockitoSerializationIssue(join(
"A class couldn't be found while deserializing a Mockito mock, you should check your classpath. The error was :",
" " + cce.getMessage(),
"If you are still unsure what is the reason of this exception, feel free to contact us on the mailing list
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>Class.getCanonicalName());
} catch (NoSuchFieldException nsfe) {
// TODO use our own mockito mock serialization exception
throw new MockitoSerializationIssue(join(
"Wow, the class 'ObjectStreamClass' in the JDK don't have the field 'name',",
"this is definitely a bug in our code as it means the JDK team changed a few internal things.",
"",
"Please report an issue with the JDK used, a code sample and a link to download the JDK would be welcome."
), nsfe);
}
}
/**
* Read the stream class annotation and identify it as a Mockito mock or not.
*
* @param marker The marker to identify.
* @return <code>true</code> if not marked as a Mockito, <code>false</code> if the class annotation marks a Mockito mock.
* @throws IOException
* @throws ClassNotFoundException
*/
private boolean notMarkedAsAMockitoMock(Object marker) throws IOException, ClassNotFoundException {
return !MOCKITO_PROXY_MARKER.equals(marker);
}
}
/**
* Special Mockito aware <code>ObjectOutputStream</code>.
*
* <p>
* This output stream has the role of marking in the stream the Mockito class. This
* marking process is necessary to identify the proxy class that will need to be recreated.
*
* The mirror method used for deserializing the mock is
* {@link MockitoMockObjectInputStream#resolveClass(ObjectStreamClass)}.
* </p>
*
*/
private static class MockitoMockObjectOutputStream extends ObjectOutputStream {
private static final String NOTHING = "";
public MockitoMockObjectOutputStream(ByteArrayOutputStream out) throws IOException {
super(out);
}
/**
* Annotates (marks) the class if this class is a Mockito mock.
*
* @param cl The class to annotate.
* @throws IOException
*/
@Override
protected void annotateClass(Class<?> cl) throws IOException {
writeObject(mockitoProxyClassMarker(cl));
// might be also useful later, for embedding classloader info ...maybe ...maybe not
}
/**
* Returns the Mockito marker if this class is a Mockito mock.
*
* @param cl The class to mark.
* @return The marker if this is a Mockito proxy class, otherwise returns
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> a void marker.
*/
private String mockitoProxyClassMarker(Class<?> cl) {
if (AcrossJVMMockitoMockSerializable.class.isAssignableFrom(cl)) {
return MOCKITO_PROXY_MARKER;
} else {
return NOTHING;
}
}
}
/**
* Simple interface that hold a correct <code>writeReplace</code> signature that can be seen by an
* <code>ObjectOutputStream</code>.
*
* It will be applied before the creation of the mock when the mock setting says it should serializable.
*
* @see #enableSerializationAcrossJVM(org.mockito.mock.MockCreationSettings)
*/
public interface AcrossJVMMockitoMockSerializable {
public Object writeReplace() throws java.io.ObjectStreamException;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.cglib;
import org.mockito.cglib.core.CodeGenerationException;
import org.mockito.cglib.core.NamingPolicy;
import org.mockito.cglib.core.Predicate;
import org.mockito.cglib.proxy.*;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.creation.instance.InstantationException;
import org.mockito.internal.creation.instance.Instantiator;
import org.mockito.internal.creation.util.SearchingClassLoader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.List;
import static org.mockito.internal.util.StringJoiner.join;
/**
* Inspired on jMock (thanks jMock guys!!!)
*/
class ClassImposterizer {
private final Instantiator instantiator;
public ClassImposterizer(Instantiator instantiator) {
this.instantiator = instantiator;
}
private static final NamingPolicy NAMING_POLICY_THAT_ALLOWS_IMPOSTERISATION_OF_CLASSES_IN_SIGNED_PACKAGES = new MockitoNamingPolicy() {
@Override
public String getClassName(String prefix, String source, Object key, Predicate names) {
return "codegen." + super.getClassName(prefix, source, key, names);
}
};
private static final CallbackFilter IGNORE_BRIDGE_METHODS = new CallbackFilter() {
public int accept(Method method) {
return method.isBridge() ? 1 : 0;
}
};
public <T> T imposterise(final MethodInterceptor interceptor, Class<T> mockedType, Collection<Class> ancillaryTypes) {
return imposterise(interceptor, mockedType, ancillaryTypes.toArray(new Class[ancillaryTypes.size()]));
}
public <T> T imposterise(final MethodInterceptor interceptor, Class<T> mockedType, Class<?>... ancillaryTypes) {
Class
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS><Factory> proxyClass = null;
Object proxyInstance = null;
try {
setConstructorsAccessible(mockedType, true);
proxyClass = createProxyClass(mockedType, ancillaryTypes);
proxyInstance = createProxy(proxyClass, interceptor);
return mockedType.cast(proxyInstance);
} catch (ClassCastException cce) {
throw new MockitoException(join(
"ClassCastException occurred while creating the mockito proxy :",
" class to mock : " + describeClass(mockedType),
" created class : " + describeClass(proxyClass),
" proxy instance class : " + describeClass(proxyInstance),
" instance creation by : " + instantiator.getClass().getSimpleName(),
"",
"You might experience classloading issues, disabling the Objenesis cache *might* help (see MockitoConfiguration)"
), cce);
} finally {
setConstructorsAccessible(mockedType, false);
}
}
private static String describeClass(Class type) {
return type == null? "null" : "'" + type.getCanonicalName() + "', loaded by classloader : '" + type.getClassLoader() + "'";
}
private static String describeClass(Object instance) {
return instance == null? "null" : describeClass(instance.getClass());
}
//TODO this method does not belong here
public void setConstructorsAccessible(Class<?> mockedType, boolean accessible) {
for (Constructor<?> constructor : mockedType.getDeclaredConstructors()) {
constructor.setAccessible(accessible);
}
}
public Class<Factory> createProxyClass(Class<?> mockedType, Class<?>... interfaces) {
if (mockedType == Object.class) {
mockedType = ClassWithSuperclassToWorkAroundCglibBug.class;
}
Enhancer enhancer = new Enhancer() {
@Override
@SuppressWarnings("unchecked")
protected void filterConstructors(Class sc, List constructors) {
// Don't filter
}
};
Class<?>[] allMockedTypes = prepend(mockedType, interfaces);
enhancer.setClassLoader(SearchingClassLoader.combineLoadersOf(allMockedTypes));
enhancer.setUseFactory(true);
if (mockedType.isInterface()) {
enhancer.setSuperclass(Object.class);
enhancer
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>.setInterfaces(allMockedTypes);
} else {
enhancer.setSuperclass(mockedType);
enhancer.setInterfaces(interfaces);
}
enhancer.setCallbackTypes(new Class[]{MethodInterceptor.class, NoOp.class});
enhancer.setCallbackFilter(IGNORE_BRIDGE_METHODS);
if (mockedType.getSigners() != null) {
enhancer.setNamingPolicy(NAMING_POLICY_THAT_ALLOWS_IMPOSTERISATION_OF_CLASSES_IN_SIGNED_PACKAGES);
} else {
enhancer.setNamingPolicy(MockitoNamingPolicy.INSTANCE);
}
enhancer.setSerialVersionUID(42L);
try {
return enhancer.createClass();
} catch (CodeGenerationException e) {
if (Modifier.isPrivate(mockedType.getModifiers())) {
throw new MockitoException("\n"
+ "Mockito cannot mock this class: " + mockedType
+ ".\n"
+ "Most likely it is a private class that is not visible by Mockito");
}
throw new MockitoException("\n"
+ "Mockito cannot mock this class: " + mockedType
+ "\n"
+ "Mockito can only mock visible & non-final classes."
+ "\n"
+ "If you're not sure why you're getting this error, please report to the mailing list.", e);
}
}
private Object createProxy(Class<Factory> proxyClass, final MethodInterceptor interceptor) {
Factory proxy;
try {
proxy = instantiator.newInstance(proxyClass);
} catch (InstantationException e) {
throw new MockitoException("Unable to create mock instance of type '" + proxyClass.getSuperclass().getSimpleName() + "'", e);
}
proxy.setCallbacks(new Callback[] {interceptor, SerializableNoOp.SERIALIZABLE_INSTANCE });
return proxy;
}
private Class<?>[] prepend(Class<?> first, Class<?>... rest) {
Class<?>[] all = new Class<?>[rest.length+1];
all[0] = first;
System.arraycopy(rest, 0, all, 1, rest.length);
return all;
}
public static class ClassWithSuperclassToWorkAroundCglibBug {}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.settings;
import org.mockito.listeners.InvocationListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.mock.MockName;
import org.mockito.mock.SerializableMode;
import org.mockito.stubbing.Answer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* by Szczepan Faber, created at: 4/9/12
*/
public class CreationSettings<T> implements MockCreationSettings<T>, Serializable {
private static final long serialVersionUID = -6789800638070123629L;
protected Class<T> typeToMock;
protected Set<Class> extraInterfaces = new LinkedHashSet<Class>();
protected String name;
protected Object spiedInstance;
protected Answer<Object> defaultAnswer;
protected MockName mockName;
protected SerializableMode serializableMode = SerializableMode.NONE;
protected List<InvocationListener> invocationListeners = new ArrayList<InvocationListener>();
protected boolean stubOnly;
private boolean useConstructor;
private Object outerClassInstance;
public CreationSettings() {}
@SuppressWarnings("unchecked")
public CreationSettings(CreationSettings copy) {
this.typeToMock = copy.typeToMock;
this.extraInterfaces = copy.extraInterfaces;
this.name = copy.name;
this.spiedInstance = copy.spiedInstance;
this.defaultAnswer = copy.defaultAnswer;
this.mockName = copy.mockName;
this.serializableMode = copy.serializableMode;
this.invocationListeners = copy.invocationListeners;
this.stubOnly = copy.stubOnly;
this.useConstructor = copy.isUsingConstructor();
this.outerClassInstance = copy.getOuterClassInstance();
}
public Class<T> getTypeToMock() {
return typeToMock;
}
public CreationSettings<T> setTypeToMock(Class<T> typeToMock) {
this.typeTo
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>Mock = typeToMock;
return this;
}
public Set<Class> getExtraInterfaces() {
return extraInterfaces;
}
public CreationSettings<T> setExtraInterfaces(Set<Class> extraInterfaces) {
this.extraInterfaces = extraInterfaces;
return this;
}
public String getName() {
return name;
}
public Object getSpiedInstance() {
return spiedInstance;
}
public Answer<Object> getDefaultAnswer() {
return defaultAnswer;
}
public MockName getMockName() {
return mockName;
}
public CreationSettings<T> setMockName(MockName mockName) {
this.mockName = mockName;
return this;
}
public boolean isSerializable() {
return serializableMode != SerializableMode.NONE;
}
public SerializableMode getSerializableMode() {
return serializableMode;
}
public List<InvocationListener> getInvocationListeners() {
return invocationListeners;
}
public boolean isUsingConstructor() {
return useConstructor;
}
public Object getOuterClassInstance() {
return outerClassInstance;
}
public boolean isStubOnly() {
return stubOnly;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.configuration;
import org.mockito.MockitoAnnotations;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
* Configures mock creation logic behind @Mock, @Captor and @Spy annotations
* <p>
* If you are interested then see implementations or source code of {@link MockitoAnnotations#initMocks(Object)}
*/
public interface AnnotationEngine {
/**
* @deprecated
* Please use {@link AnnotationEngine#process(Class, Object)} method instead that is more robust
* <p>
* Creates mock, ArgumentCaptor or wraps field instance in spy object.
* Only if of correct annotation type.
*
* @param annotation Annotation
* @param field Field details
*/
@Deprecated
Object createMockFor(Annotation annotation, Field field);
/**
* Allows extending the interface to perform action on specific fields on the test class.
* <p>
* See the implementation of this method to figure out what is it for.
*
* @param clazz Class where to extract field information, check implementation for details
* @param testInstance Test instance
*/
void process(Class<?> clazz, Object testInstance);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> Captor}, @{@link InjectMocks}
* <p>
* <b><code>MockitoAnnotations.initMocks(this)</code></b> method has to called to initialize annotated fields.
* <p>
* In above example, <code>initMocks()</code> is called in @Before (JUnit4) method of test's base class.
* For JUnit3 <code>initMocks()</code> can go to <code>setup()</code> method of a base class.
* You can also put initMocks() in your JUnit runner (@RunWith) or use built-in runner: {@link MockitoJUnitRunner}
*/
public class MockitoAnnotations {
/**
* Use top-level {@link org.mockito.Mock} annotation instead
* <p>
* When @Mock annotation was implemented as an inner class then users experienced problems with autocomplete features in IDEs.
* Hence @Mock was made a top-level class.
* <p>
* How to fix deprecation warnings?
* Typically, you can just <b>search:</b> import org.mockito.MockitoAnnotations.Mock; <b>and replace with:</b> import org.mockito.Mock;
* <p>
* If you're an existing user then sorry for making your code littered with deprecation warnings.
* This change was required to make Mockito better.
*/
@Target( { FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Deprecated
public @interface Mock {}
/**
* Initializes objects annotated with Mockito annotations for given testClass:
* @{@link org.mockito.Mock}, @{@link Spy}, @{@link Captor}, @{@link InjectMocks}
* <p>
* See examples in javadoc for {@link MockitoAnnotations} class.
*/
public static void initMocks(Object testClass) {
if (testClass == null) {
throw new MockitoException("testClass cannot be null. For info how to use @Mock annotations see examples in javadoc for MockitoAnnotations class");
}
AnnotationEngine annotationEngine = new GlobalConfiguration().getAnnotationEngine();
Class<?> clazz = testClass.getClass();
//below can be removed later, when we get
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> read rid of deprecated stuff
if (annotationEngine.getClass() != new DefaultMockitoConfiguration().getAnnotationEngine().getClass()) {
//this means user has his own annotation engine and we have to respect that.
//we will do annotation processing the old way so that we are backwards compatible
while (clazz != Object.class) {
scanDeprecatedWay(annotationEngine, testClass, clazz);
clazz = clazz.getSuperclass();
}
}
//anyway act 'the new' way
annotationEngine.process(testClass.getClass(), testClass);
}
static void scanDeprecatedWay(AnnotationEngine annotationEngine, Object testClass, Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
processAnnotationDeprecatedWay(annotationEngine, testClass, field);
}
}
@SuppressWarnings("deprecation")
static void processAnnotationDeprecatedWay(AnnotationEngine annotationEngine, Object testClass, Field field) {
boolean alreadyAssigned = false;
for(Annotation annotation : field.getAnnotations()) {
Object mock = annotationEngine.createMockFor(annotation, field);
if (mock != null) {
throwIfAlreadyAssigned(field, alreadyAssigned);
alreadyAssigned = true;
try {
new FieldSetter(testClass, field).set(mock);
} catch (Exception e) {
throw new MockitoException("Problems setting field " + field.getName() + " annotated with "
+ annotation, e);
}
}
}
}
static void throwIfAlreadyAssigned(Field field, boolean alreadyAssigned) {
if (alreadyAssigned) {
new Reporter().moreThanOneAnnotationNotAllowed(field.getName());
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection.filter;
/**
* Allow the ongoing injection of a mock candidate.
*/
public interface OngoingInjecter {
/**
* Inject the mock.
*
* <p>
* Please check the actual implementation.
* </p>
*
* @return the mock that was injected, <code>null</code> otherwise.
*/
Object thenInject();
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.matchers;
import static org.mockito.AdditionalMatchers.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.mockito.exceptions.verification.WantedButNotInvoked;
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;
@SuppressWarnings("unchecked")
public class MatchersTest extends TestBase {
private IMethods mock;
@Before
public void setUp() {
mock = Mockito.mock(IMethods.class);
}
@Test
public void andOverloaded() {
when(mock.oneArg(and(eq(false), eq(false)))).thenReturn("0");
when(mock.oneArg(and(eq((byte) 1), eq((byte) 1)))).thenReturn("1");
when(mock.oneArg(and(eq('a'), eq('a')))).thenReturn("2");
when(mock.oneArg(and(eq((double) 1), eq((double) 1)))).thenReturn("3");
when(mock.oneArg(and(eq((float) 1), eq((float) 1)))).thenReturn("4");
when(mock.oneArg(and(eq((int) 1), eq((int) 1)))).thenReturn("5");
when(mock.oneArg(and(eq((long) 1), eq((long) 1)))).thenReturn("6");
when(mock.oneArg(and(eq((short) 1), eq((short) 1)))).thenReturn("7");
when(mock.oneArg(and(Matchers.contains("a"), Matchers.contains("d")))).thenReturn("8");
when(mock.oneArg(and(isA(Class.class), eq(Object.class)))).
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>thenReturn("9");
assertEquals("0", mock.oneArg(false));
assertEquals(null, mock.oneArg(true));
assertEquals("1", mock.oneArg((byte) 1));
assertEquals("2", mock.oneArg('a'));
assertEquals("3", mock.oneArg((double) 1));
assertEquals("4", mock.oneArg((float) 1));
assertEquals("5", mock.oneArg((int) 1));
assertEquals("6", mock.oneArg((long) 1));
assertEquals("7", mock.oneArg((short) 1));
assertEquals("8", mock.oneArg("abcde"));
assertEquals(null, mock.oneArg("aaaaa"));
assertEquals("9", mock.oneArg(Object.class));
}
@Test
public void orOverloaded() {
when(mock.oneArg(or(eq(false), eq(true)))).thenReturn("0");
when(mock.oneArg(or(eq((byte) 1), eq((byte) 2)))).thenReturn("1");
when(mock.oneArg(or(eq((char) 1), eq((char) 2)))).thenReturn("2");
when(mock.oneArg(or(eq((double) 1), eq((double) 2)))).thenReturn("3");
when(mock.oneArg(or(eq((float) 1), eq((float) 2)))).thenReturn("4");
when(mock.oneArg(or(eq((int) 1), eq((int) 2)))).thenReturn("5");
when(mock.oneArg(or(eq((long) 1), eq((long) 2)))).thenReturn("6");
when(mock.oneArg(or(eq((short) 1), eq((short) 2)))).thenReturn("7");
when(mock.oneArg(or(eq("asd"), eq("jkl")))).thenReturn("8");
when(mock.oneArg(or(eq(this.getClass()), eq(Object.class)))).thenReturn("9");
assertEquals("0", mock.oneArg(true));
assertEquals("0", mock.oneArg(false));
assertEquals("1", mock.oneArg((byte) 2));
assertEquals("2", mock.oneArg((
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>char) 1));
assertEquals("3", mock.oneArg((double) 2));
assertEquals("4", mock.oneArg((float) 1));
assertEquals("5", mock.oneArg((int) 2));
assertEquals("6", mock.oneArg((long) 1));
assertEquals("7", mock.oneArg((short) 1));
assertEquals("8", mock.oneArg("jkl"));
assertEquals("8", mock.oneArg("asd"));
assertEquals(null, mock.oneArg("asdjkl"));
assertEquals("9", mock.oneArg(Object.class));
assertEquals(null, mock.oneArg(String.class));
}
@Test
public void notOverloaded() {
when(mock.oneArg(not(eq(false)))).thenReturn("0");
when(mock.oneArg(not(eq((byte) 1)))).thenReturn("1");
when(mock.oneArg(not(eq('a')))).thenReturn("2");
when(mock.oneArg(not(eq((double) 1)))).thenReturn("3");
when(mock.oneArg(not(eq((float) 1)))).thenReturn("4");
when(mock.oneArg(not(eq((int) 1)))).thenReturn("5");
when(mock.oneArg(not(eq((long) 1)))).thenReturn("6");
when(mock.oneArg(not(eq((short) 1)))).thenReturn("7");
when(mock.oneArg(not(Matchers.contains("a")))).thenReturn("8");
when(mock.oneArg(not(isA(Class.class)))).thenReturn("9");
assertEquals("0", mock.oneArg(true));
assertEquals(null, mock.oneArg(false));
assertEquals("1", mock.oneArg((byte) 2));
assertEquals("2", mock.oneArg('b'));
assertEquals("3", mock.oneArg((double) 2));
assertEquals("4", mock.oneArg((float) 2));
assertEquals("5", mock.oneArg((int) 2));
assertEquals("6", mock.oneArg((long) 2));
assertEquals("7", mock.oneArg((short) 2));
assertEquals("8", mock.oneArg("
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>bcde"));
assertEquals("9", mock.oneArg(new Object()));
assertEquals(null, mock.oneArg(Class.class));
}
@Test
public void lessOrEqualOverloaded() {
when(mock.oneArg(leq((byte) 1))).thenReturn("1");
when(mock.oneArg(leq((double) 1))).thenReturn("3");
when(mock.oneArg(leq((float) 1))).thenReturn("4");
when(mock.oneArg(leq((int) 1))).thenReturn("5");
when(mock.oneArg(leq((long) 1))).thenReturn("6");
when(mock.oneArg(leq((short) 1))).thenReturn("7");
when(mock.oneArg(leq(new BigDecimal("1")))).thenReturn("8");
assertEquals("1", mock.oneArg((byte) 1));
assertEquals(null, mock.oneArg((byte) 2));
assertEquals("3", mock.oneArg((double) 1));
assertEquals("7", mock.oneArg((short) 0));
assertEquals("4", mock.oneArg((float) -5));
assertEquals("5", mock.oneArg((int) -2));
assertEquals("6", mock.oneArg((long) -3));
assertEquals("8", mock.oneArg(new BigDecimal("0.5")));
assertEquals(null, mock.oneArg(new BigDecimal("1.1")));
}
@Test
public void lessThanOverloaded() {
when(mock.oneArg(lt((byte) 1))).thenReturn("1");
when(mock.oneArg(lt((double) 1))).thenReturn("3");
when(mock.oneArg(lt((float) 1))).thenReturn("4");
when(mock.oneArg(lt((int) 1))).thenReturn("5");
when(mock.oneArg(lt((long) 1))).thenReturn("6");
when(mock.oneArg(lt((short) 1))).thenReturn("7");
when(mock.oneArg(lt(new BigDecimal("1")))).thenReturn("8");
assertEquals("1", mock.oneArg((byte) 0));
assertEquals(null, mock.oneArg((byte) 1));
assertEquals("3", mock.oneArg((double) 0
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>thenReturn("7");
when(mock.oneArg(gt(new BigDecimal("1")))).thenReturn("8");
assertEquals("1", mock.oneArg((byte) 2));
assertEquals(null, mock.oneArg((byte) 1));
assertEquals("3", mock.oneArg((double) 2));
assertEquals("7", mock.oneArg((short) 2));
assertEquals("4", mock.oneArg((float) 3));
assertEquals("5", mock.oneArg((int) 2));
assertEquals("6", mock.oneArg((long) 5));
assertEquals("8", mock.oneArg(new BigDecimal("1.5")));
assertEquals(null, mock.oneArg(new BigDecimal("0.9")));
}
@Test
public void compareToMatcher() {
when(mock.oneArg(cmpEq(new BigDecimal("1.5")))).thenReturn("0");
assertEquals("0", mock.oneArg(new BigDecimal("1.50")));
assertEquals(null, mock.oneArg(new BigDecimal("1.51")));
}
@Test
public void anyStringMatcher() {
when(mock.oneArg(anyString())).thenReturn("matched");
assertEquals("matched", mock.oneArg(""));
assertEquals("matched", mock.oneArg("any string"));
assertEquals(null, mock.oneArg((String) null));
}
@Test
public void anyMatcher() {
when(mock.forObject(any())).thenReturn("matched");
assertEquals("matched", mock.forObject(123));
assertEquals("matched", mock.forObject("any string"));
assertEquals("matched", mock.forObject("any string"));
assertEquals("matched", mock.forObject((Object) null));
}
@Test
public void anyXMatcher() {
when(mock.oneArg(anyBoolean())).thenReturn("0");
when(mock.oneArg(anyByte())).thenReturn("1");
when(mock.oneArg(anyChar())).thenReturn("2");
when(mock.oneArg(anyDouble())).thenReturn("3");
when(mock.oneArg(anyFloat())).thenReturn("4");
when(mock.oneArg(anyInt())).thenReturn("5");
when(mock.oneArg(anyLong())).thenReturn("6");
when(mock.oneArg(anyShort
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>())).thenReturn("7");
when(mock.oneArg((String) anyObject())).thenReturn("8");
when(mock.oneArg(anyObject())).thenReturn("9");
assertEquals("0", mock.oneArg(true));
assertEquals("0", mock.oneArg(false));
assertEquals("1", mock.oneArg((byte) 1));
assertEquals("2", mock.oneArg((char) 1));
assertEquals("3", mock.oneArg((double) 1));
assertEquals("4", mock.oneArg((float) 889));
assertEquals("5", mock.oneArg((int) 1));
assertEquals("6", mock.oneArg((long) 1));
assertEquals("7", mock.oneArg((short) 1));
assertEquals("8", mock.oneArg("Test"));
assertEquals("9", mock.oneArg(new Object()));
assertEquals("9", mock.oneArg(new HashMap()));
}
@Test
public void shouldArrayEqualsDealWithNullArray() throws Exception {
Object[] nullArray = null;
when(mock.oneArray(aryEq(nullArray))).thenReturn("null");
assertEquals("null", mock.oneArray(nullArray));
mock = mock(IMethods.class);
try {
verify(mock).oneArray(aryEq(nullArray));
fail();
} catch (WantedButNotInvoked e) {
assertContains("oneArray(null)", e.getMessage());
}
}
@Test
public void shouldUseSmartEqualsForArrays() throws Exception {
//issue 143
mock.arrayMethod(new String[] {"one"});
verify(mock).arrayMethod(eq(new String[] {"one"}));
verify(mock).arrayMethod(new String[] {"one"});
}
@Test
public void shouldUseSmartEqualsForPrimitiveArrays() throws Exception {
//issue 143
mock.objectArgMethod(new int[] {1, 2});
verify(mock).objectArgMethod(eq(new int[] {1, 2}));
verify(mock).objectArgMethod(new int[] {1, 2});
}
@Test(expected=ArgumentsAreDifferent.class)
public void arrayEqualsShouldThrowArgumentsAreDifferentExceptionForNonMatchingArguments() {
List list = Mockito.mock(List
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>.class);
list.add("test"); // testing fix for issue 20
list.contains(new Object[] {"1"});
Mockito.verify(list).contains(new Object[] {"1", "2", "3"});
}
@Test
public void arrayEqualsMatcher() {
when(mock.oneArray(aryEq(new boolean[] { true, false, false }))).thenReturn("0");
when(mock.oneArray(aryEq(new byte[] { 1 }))).thenReturn("1");
when(mock.oneArray(aryEq(new char[] { 1 }))).thenReturn("2");
when(mock.oneArray(aryEq(new double[] { 1 }))).thenReturn("3");
when(mock.oneArray(aryEq(new float[] { 1 }))).thenReturn("4");
when(mock.oneArray(aryEq(new int[] { 1 }))).thenReturn("5");
when(mock.oneArray(aryEq(new long[] { 1 }))).thenReturn("6");
when(mock.oneArray(aryEq(new short[] { 1 }))).thenReturn("7");
when(mock.oneArray(aryEq(new String[] { "Test" }))).thenReturn("8");
when(mock.oneArray(aryEq(new Object[] { "Test", new Integer(4) }))).thenReturn("9");
assertEquals("0", mock.oneArray(new boolean[] { true, false, false }));
assertEquals("1", mock.oneArray(new byte[] { 1 }));
assertEquals("2", mock.oneArray(new char[] { 1 }));
assertEquals("3", mock.oneArray(new double[] { 1 }));
assertEquals("4", mock.oneArray(new float[] { 1 }));
assertEquals("5", mock.oneArray(new int[] { 1 }));
assertEquals("6", mock.oneArray(new long[] { 1 }));
assertEquals("7", mock.oneArray(new short[] { 1 }));
assertEquals("8", mock.oneArray(new String[] { "Test" }));
assertEquals("9", mock.oneArray(new Object[] { "Test", new Integer(4) }));
assertEquals(null, mock.oneArray(new Object[] { "Test", new Integer(999) }));
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> assertEquals(null, mock.oneArray(new Object[] { "Test", new Integer(4), "x" }));
assertEquals(null, mock.oneArray(new boolean[] { true, false }));
assertEquals(null, mock.oneArray(new boolean[] { true, true, false }));
}
@Test
public void greaterOrEqualMatcher() {
when(mock.oneArg(geq(7))).thenReturn(">= 7");
when(mock.oneArg(lt(7))).thenReturn("< 7");
assertEquals(">= 7", mock.oneArg(7));
assertEquals(">= 7", mock.oneArg(8));
assertEquals(">= 7", mock.oneArg(9));
assertEquals("< 7", mock.oneArg(6));
assertEquals("< 7", mock.oneArg(6));
}
@Test
public void greaterThanMatcher() {
when(mock.oneArg(gt(7))).thenReturn("> 7");
when(mock.oneArg(leq(7))).thenReturn("<= 7");
assertEquals("> 7", mock.oneArg(8));
assertEquals("> 7", mock.oneArg(9));
assertEquals("> 7", mock.oneArg(10));
assertEquals("<= 7", mock.oneArg(7));
assertEquals("<= 7", mock.oneArg(6));
}
@Test
public void lessOrEqualMatcher() {
when(mock.oneArg(leq(7))).thenReturn("<= 7");
when(mock.oneArg(gt(7))).thenReturn("> 7");
assertEquals("<= 7", mock.oneArg(7));
assertEquals("<= 7", mock.oneArg(6));
assertEquals("<= 7", mock.oneArg(5));
assertEquals("> 7", mock.oneArg(8));
assertEquals("> 7", mock.oneArg(9));
}
@Test
public void lessThanMatcher() {
when(mock.oneArg(lt(7))).thenReturn("< 7");
when(mock.oneArg(geq(7))).thenReturn(">= 7");
assertEquals("< 7", mock.oneArg(5));
assertEquals("< 7", mock.oneArg(6));
assertEquals("< 7", mock.oneArg(4));
assertEquals(">= 7", mock.oneArg
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>(7));
assertEquals(">= 7", mock.oneArg(8));
}
@Test
public void orMatcher() {
when(mock.oneArg(anyInt())).thenReturn("other");
when(mock.oneArg(or(eq(7), eq(9)))).thenReturn("7 or 9");
assertEquals("other", mock.oneArg(10));
assertEquals("7 or 9", mock.oneArg(7));
assertEquals("7 or 9", mock.oneArg(9));
}
@Test
public void nullMatcher() {
when(mock.threeArgumentMethod(eq(1), isNull(), eq(""))).thenReturn("1");
when(mock.threeArgumentMethod(eq(1), not(isNull()), eq(""))).thenReturn("2");
assertEquals("1", mock.threeArgumentMethod(1, null, ""));
assertEquals("2", mock.threeArgumentMethod(1, new Object(), ""));
}
@Test
public void notNullMatcher() {
when(mock.threeArgumentMethod(eq(1), notNull(), eq(""))).thenReturn("1");
when(mock.threeArgumentMethod(eq(1), not(isNotNull()), eq(""))).thenReturn("2");
assertEquals("1", mock.threeArgumentMethod(1, new Object(), ""));
assertEquals("2", mock.threeArgumentMethod(1, null, ""));
}
@Test
public void findMatcher() {
when(mock.oneArg(find("([a-z]+)\\d"))).thenReturn("1");
assertEquals("1", mock.oneArg("ab12"));
assertEquals(null, mock.oneArg("12345"));
assertEquals(null, mock.oneArg((Object) null));
}
@Test
public void matchesMatcher() {
when(mock.oneArg(matches("[a-z]+\\d\\d"))).thenReturn("1");
when(mock.oneArg(matches("\\d\\d\\d"))).thenReturn("2");
assertEquals("1", mock.oneArg("a12"));
assertEquals("2", mock.oneArg("131"));
assertEquals(null, mock.oneArg("blah"));
}
@Test
public void containsMatcher() {
when(mock.oneArg(Matchers.contains("ell"))).thenReturn("1");
when(mock
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>MatcherPrintsItself() {
try {
verify(mock).oneArg(eq(1.0D, 0.1D));
fail();
} catch (WantedButNotInvoked e) {
assertContains("eq(1.0, 0.1)", e.getMessage());
}
}
@Test
public void sameMatcher() {
Object one = new String("1243");
Object two = new String("1243");
Object three = new String("1243");
assertNotSame(one, two);
assertEquals(one, two);
assertEquals(two, three);
when(mock.oneArg(same(one))).thenReturn("1");
when(mock.oneArg(same(two))).thenReturn("2");
assertEquals("1", mock.oneArg(one));
assertEquals("2", mock.oneArg(two));
assertEquals(null, mock.oneArg(three));
}
@Test
public void eqMatcherAndNulls() {
mock.simpleMethod((Object) null);
verify(mock).simpleMethod((Object) eq(null));
}
@Test
public void sameMatcherAndNulls() {
mock.simpleMethod((Object) null);
verify(mock).simpleMethod(same(null));
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
import org.mockito.Mockito;
import org.mockito.exceptions.misusing.NotAMockException;
import org.mockito.internal.InternalMockHandler;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.creation.settings.CreationSettings;
import org.mockito.internal.handler.MockHandlerFactory;
import org.mockito.internal.util.reflection.LenientCopyTool;
import org.mockito.invocation.MockHandler;
import org.mockito.mock.MockCreationSettings;
import org.mockito.mock.MockName;
import org.mockito.plugins.MockMaker;
import java.lang.reflect.Modifier;
@SuppressWarnings("unchecked")
public class MockUtil {
private static final MockMaker mockMaker = Plugins.getMockMaker();
public boolean isTypeMockable(Class<?> type) {
return !type.isPrimitive() && !Modifier.isFinal(type.getModifiers());
}
public <T> T createMock(MockCreationSettings<T> settings) {
MockHandler mockHandler = new MockHandlerFactory().create(settings);
T mock = mockMaker.createMock(settings, mockHandler);
Object spiedInstance = settings.getSpiedInstance();
if (spiedInstance != null) {
new LenientCopyTool().copyToMock(spiedInstance, mock);
}
return mock;
}
public <T> void resetMock(T mock) {
InternalMockHandler oldHandler = (InternalMockHandler) getMockHandler(mock);
MockCreationSettings settings = oldHandler.getMockSettings();
MockHandler newHandler = new MockHandlerFactory().create(settings);
mockMaker.resetMock(mock, newHandler, settings);
}
public <T> InternalMockHandler<T> getMockHandler(T mock) {
if (mock == null) {
throw new NotAMockException("Argument should be a mock, but is null!");
}
if (isMockitoMock(mock)) {
MockHandler handler = mockMaker.getHandler(mock);
return (InternalMockHandler) handler;
} else {
throw new Not
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>AMockException("Argument should be a mock, but is: " + mock.getClass());
}
}
public boolean isMock(Object mock) {
// double check to avoid classes that have the same interfaces, could be great to have a custom mockito field in the proxy instead of relying on instance fields
return isMockitoMock(mock);
}
public boolean isSpy(Object mock) {
return isMockitoMock(mock) && getMockSettings(mock).getDefaultAnswer() == Mockito.CALLS_REAL_METHODS;
}
private <T> boolean isMockitoMock(T mock) {
return mockMaker.getHandler(mock) != null;
}
public MockName getMockName(Object mock) {
return getMockHandler(mock).getMockSettings().getMockName();
}
public void maybeRedefineMockName(Object mock, String newName) {
MockName mockName = getMockName(mock);
//TODO SF hacky...
if (mockName.isDefault() && getMockHandler(mock).getMockSettings() instanceof CreationSettings) {
((CreationSettings) getMockHandler(mock).getMockSettings()).setMockName(new MockNameImpl(newName));
}
}
public MockCreationSettings getMockSettings(Object mock) {
return getMockHandler(mock).getMockSettings();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.progress;
import org.mockito.internal.listeners.MockingProgressListener;
import org.mockito.invocation.Invocation;
import org.mockito.verification.VerificationMode;
import java.io.Serializable;
@SuppressWarnings("unchecked")
public class ThreadSafeMockingProgress implements MockingProgress, Serializable {
private static final long serialVersionUID = 6839454041642082618L;
private static final ThreadLocal<MockingProgress> mockingProgress = new ThreadLocal<MockingProgress>();
static MockingProgress threadSafely() {
if (mockingProgress.get() == null) {
mockingProgress.set(new MockingProgressImpl());
}
return mockingProgress.get();
}
public void reportOngoingStubbing(IOngoingStubbing iOngoingStubbing) {
threadSafely().reportOngoingStubbing(iOngoingStubbing);
}
public IOngoingStubbing pullOngoingStubbing() {
return threadSafely().pullOngoingStubbing();
}
public void verificationStarted(VerificationMode verify) {
threadSafely().verificationStarted(verify);
}
public VerificationMode pullVerificationMode() {
return threadSafely().pullVerificationMode();
}
public void stubbingStarted() {
threadSafely().stubbingStarted();
}
public void validateState() {
threadSafely().validateState();
}
public void stubbingCompleted(Invocation invocation) {
threadSafely().stubbingCompleted(invocation);
}
public String toString() {
return threadSafely().toString();
}
public void reset() {
threadSafely().reset();
}
public void resetOngoingStubbing() {
threadSafely().resetOngoingStubbing();
}
public ArgumentMatcherStorage getArgumentMatcherStorage() {
return threadSafely().getArgumentMatcherStorage();
}
public void mockingStarted(Object mock, Class classToMock) {
threadSafely().mockingStarted(mock, classToMock);
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection.filter;
import org.mockito.internal.util.MockUtil;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class NameBasedCandidateFilter implements MockCandidateFilter {
private final MockCandidateFilter next;
private final MockUtil mockUtil = new MockUtil();
public NameBasedCandidateFilter(MockCandidateFilter next) {
this.next = next;
}
public OngoingInjecter filterCandidate(Collection<Object> mocks, Field field, Object fieldInstance) {
List<Object> mockNameMatches = new ArrayList<Object>();
if(mocks.size() > 1) {
for (Object mock : mocks) {
if (field.getName().equals(mockUtil.getMockName(mock).toString())) {
mockNameMatches.add(mock);
}
}
return next.filterCandidate(mockNameMatches, field, fieldInstance);
}
return next.filterCandidate(mocks, field, fieldInstance);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation;
import org.mockito.internal.invocation.MockitoMethod;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class DelegatingMethod implements MockitoMethod {
private final Method method;
public DelegatingMethod(Method method) {
assert method != null : "Method cannot be null";
this.method = method;
}
public Class<?>[] getExceptionTypes() {
return method.getExceptionTypes();
}
public Method getJavaMethod() {
return method;
}
public String getName() {
return method.getName();
}
public Class<?>[] getParameterTypes() {
return method.getParameterTypes();
}
public Class<?> getReturnType() {
return method.getReturnType();
}
public boolean isVarArgs() {
return method.isVarArgs();
}
public boolean isAbstract() {
return (method.getModifiers() & Modifier.ABSTRACT) != 0;
}
/**
* @return True if the input object is a DelegatingMethod which has an internal Method which is equal to the internal Method of this DelegatingMethod,
* or if the input object is a Method which is equal to the internal Method of this DelegatingMethod.
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof DelegatingMethod) {
DelegatingMethod that = (DelegatingMethod) o;
return method.equals(that.method);
} else {
return method.equals(o);
}
}
@Override
public int hashCode() {
return method.hashCode();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection.filter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class TypeBasedCandidateFilter implements MockCandidateFilter {
MockCandidateFilter next;
public TypeBasedCandidateFilter(MockCandidateFilter next) {
this.next = next;
}
public OngoingInjecter filterCandidate(Collection<Object> mocks, Field field, Object fieldInstance) {
List<Object> mockTypeMatches = new ArrayList<Object>();
for (Object mock : mocks) {
if (field.getType().isAssignableFrom(mock.getClass())) {
mockTypeMatches.add(mock);
}
}
return next.filterCandidate(mockTypeMatches, field, fieldInstance);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>package org.mockito.internal.configuration.plugins;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.misusing.MockitoConfigurationException;
import org.mockito.internal.util.collections.Iterables;
import org.mockito.plugins.PluginSwitch;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
class PluginLoader {
private final PluginSwitch pluginSwitch;
public PluginLoader(PluginSwitch pluginSwitch) {
this.pluginSwitch = pluginSwitch;
}
/**
* Scans the classpath for given pluginType. If not found, default class is used.
*/
<T> T loadPlugin(Class<T> pluginType, String defaultPluginClassName) {
T plugin = loadImpl(pluginType);
if (plugin != null) {
return plugin;
}
try {
// Default implementation. Use our own ClassLoader instead of the context
// ClassLoader, as the default implementation is assumed to be part of
// Mockito and may not be available via the context ClassLoader.
return pluginType.cast(Class.forName(defaultPluginClassName).newInstance());
} catch (Exception e) {
throw new MockitoException("Internal problem occurred, please report it. " +
"Mockito is unable to load the default implementation of class that is a part of Mockito distribution. " +
"Failed to load " + pluginType, e);
}
}
/**
* Equivalent to {@link java.util.ServiceLoader#load} but without requiring
* Java 6 / Android 2.3 (Gingerbread).
*/
<T> T loadImpl(Class<T> service) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
Enumeration<URL> resources;
try {
resources = loader.getResources("mockito-extensions/" + service.getName());
} catch (IOException e) {
throw new MockitoException("Failed to load " + service, e);
}
try {
String foundPluginClass = new PluginFinder(pluginSwitch).findPluginClass(Iterables.toIterable(resources));
if (foundPluginClass != null) {
Class<?> pluginClass = loader.loadClass(foundPluginClass);
Object plugin = pluginClass.newInstance
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.util;
import static java.lang.Thread.*;
import java.util.ArrayList;
import java.util.List;
/**
* Inspired on jMock (thanks jMock guys!!!)
*/
public class SearchingClassLoader extends ClassLoader {
private final ClassLoader nextToSearch;
public SearchingClassLoader(ClassLoader parent, ClassLoader nextToSearch) {
super(parent);
this.nextToSearch = nextToSearch;
}
public static ClassLoader combineLoadersOf(Class<?>... classes) {
return combineLoadersOf(classes[0], classes);
}
private static ClassLoader combineLoadersOf(Class<?> first, Class<?>... others) {
List<ClassLoader> loaders = new ArrayList<ClassLoader>();
addIfNewElement(loaders, first.getClassLoader());
for (Class<?> c : others) {
addIfNewElement(loaders, c.getClassLoader());
}
// To support Eclipse Plug-in tests.
// In an Eclipse plug-in, we will not be on the system class loader
// but in the class loader of the plug-in.
//
// Note: I've been unable to reproduce the error in the test suite.
addIfNewElement(loaders, SearchingClassLoader.class.getClassLoader());
// To support the Maven Surefire plugin.
// Note: I've been unable to reproduce the error in the test suite.
addIfNewElement(loaders, currentThread().getContextClassLoader());
//Had to comment that out because it didn't work with in-container Spring tests
//addIfNewElement(loaders, ClassLoader.getSystemClassLoader());
return combine(loaders);
}
private static ClassLoader combine(List<ClassLoader> parentLoaders) {
ClassLoader loader = parentLoaders.get(parentLoaders.size()-1);
for (int i = parentLoaders.size()-2; i >= 0; i--) {
loader = new SearchingClassLoader(parentLoaders.get(i), loader);
}
return loader;
}
private static void addIfNewElement(List<ClassLoader> loaders, ClassLoader c) {
if (c != null && !loaders.contains(c)) {
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers;
import org.hamcrest.Description;
import org.hamcrest.SelfDescribing;
import org.mockito.ArgumentMatcher;
import java.io.Serializable;
public class Equals extends ArgumentMatcher<Object> implements ContainsExtraTypeInformation, Serializable {
private static final long serialVersionUID = -3395637450058086891L;
private final Object wanted;
public Equals(Object wanted) {
this.wanted = wanted;
}
public boolean matches(Object actual) {
return Equality.areEqual(this.wanted, actual);
}
public void describeTo(Description description) {
description.appendText(describe(wanted));
}
public String describe(Object object) {
return quoting() + object + quoting();
}
private String quoting() {
if (wanted instanceof String) {
return "\"";
} else if (wanted instanceof Character) {
return "'";
} else {
return "";
}
}
protected final Object getWanted() {
return wanted;
}
@Override
public boolean equals(Object o) {
if (o == null || !this.getClass().equals(o.getClass())) {
return false;
}
Equals other = (Equals) o;
return this.wanted == null && other.wanted == null || this.wanted != null && this.wanted.equals(other.wanted);
}
@Override
public int hashCode() {
return 1;
}
public SelfDescribing withExtraTypeInfo() {
return new SelfDescribing() {
public void describeTo(Description description) {
description.appendText(describe("("+ wanted.getClass().getSimpleName() +") " + wanted));
}};
}
public boolean typeMatches(Object object) {
return wanted != null && object != null && object.getClass() == wanted.getClass();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("unchecked")
public class Primitives {
private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES = new HashMap<Class<?>, Class<?>>();
private static final Map<Class<?>, Object> PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES = new HashMap<Class<?>, Object>();
/**
* Returns the primitive type of the given class.
* <p/>
* The passed class can be any class : <code>boolean.class</code>, <code>Integer.class</code>
* in witch case this method will return <code>boolean.class</code>, even <code>SomeObject.class</code>
* in which case <code>null</code> will be returned.
*
* @param clazz The class from which primitive type has to be retrieved
* @param <T> The type
* @return The primitive type if relevant, otherwise <code>null</code>
*/
public static <T> Class<T> primitiveTypeOf(Class<T> clazz) {
if (clazz.isPrimitive()) {
return clazz;
}
return (Class<T>) PRIMITIVE_TYPES.get(clazz);
}
/**
* Indicates if the given class is primitive type or a primitive wrapper.
*
* @param type The type to check
* @return <code>true</code> if primitive or wrapper, <code>false</code> otherwise.
*/
public static boolean isPrimitiveOrWrapper(Class<?> type) {
return PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.containsKey(type);
}
/**
* Returns the boxed default value for a primitive or a primitive wrapper.
*
* @param primitiveOrWrapperType The type to lookup the default value
* @return The boxed default values as defined in Java Language Specification,
* <code>null</code> if the type is neither a primitive nor a wrapper
*/
public static <T> T defaultValueForPrimitiveOrWrapper(Class<T> primitiveOrWrapperType) {
return (T)
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.get(primitiveOrWrapperType);
}
static {
PRIMITIVE_TYPES.put(Boolean.class, Boolean.TYPE);
PRIMITIVE_TYPES.put(Character.class, Character.TYPE);
PRIMITIVE_TYPES.put(Byte.class, Byte.TYPE);
PRIMITIVE_TYPES.put(Short.class, Short.TYPE);
PRIMITIVE_TYPES.put(Integer.class, Integer.TYPE);
PRIMITIVE_TYPES.put(Long.class, Long.TYPE);
PRIMITIVE_TYPES.put(Float.class, Float.TYPE);
PRIMITIVE_TYPES.put(Double.class, Double.TYPE);
}
static {
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Boolean.class, false);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Character.class, '\u0000');
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Byte.class, (byte) 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Short.class, (short) 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Integer.class, 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Long.class, 0L);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Float.class, 0F);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Double.class, 0D);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(boolean.class, false);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(char.class, '\u0000');
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(byte.class, (byte) 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(short.class, (short) 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(int.class, 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(long.class, 0L);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>(float.class, 0F);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(double.class, 0D);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation;
import org.mockito.exceptions.Reporter;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.exceptions.VerificationAwareInvocation;
import org.mockito.internal.invocation.realmethod.RealMethod;
import org.mockito.internal.reporting.PrintSettings;
import org.mockito.invocation.*;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* Method call on a mock object.
* <p>
* Contains sequence number which should be globally unique and is used for
* verification in order.
* <p>
* Contains stack trace of invocation
*/
@SuppressWarnings("unchecked")
public class InvocationImpl implements Invocation, VerificationAwareInvocation {
private static final long serialVersionUID = 8240069639250980199L;
private final int sequenceNumber;
private final Object mock;
private final MockitoMethod method;
private final Object[] arguments;
private final Object[] rawArguments;
private final Location location;
private boolean verified;
private boolean isIgnoredForVerification;
final RealMethod realMethod;
private StubInfo stubInfo;
public InvocationImpl(Object mock, MockitoMethod mockitoMethod, Object[] args, int sequenceNumber, RealMethod realMethod) {
this.method = mockitoMethod;
this.mock = mock;
this.realMethod = realMethod;
this.arguments = ArgumentsProcessor.expandVarArgs(mockitoMethod.isVarArgs(), args);
this.rawArguments = args;
this.sequenceNumber = sequenceNumber;
this.location = new LocationImpl();
}
public Object getMock() {
return mock;
}
public Method getMethod() {
return method.getJavaMethod();
}
public Object[] getArguments() {
return arguments;
}
public <T> T getArgumentAt(int index, Class<T> clazz) {
return (T) arguments[index];
}
public boolean isVerified() {
return verified || isIgnoredForVerification;
}
public int getSequenceNumber() {
return sequenceNumber;
}
public
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> boolean equals(Object o) {
if (o == null || !o.getClass().equals(this.getClass())) {
return false;
}
InvocationImpl other = (InvocationImpl) o;
return this.mock.equals(other.mock) && this.method.equals(other.method) && this.equalArguments(other.arguments);
}
private boolean equalArguments(Object[] arguments) {
return Arrays.equals(arguments, this.arguments);
}
@Override
public int hashCode() {
return 1;
}
public String toString() {
return new PrintSettings().print(ArgumentsProcessor.argumentsToMatchers(getArguments()), this);
}
public Location getLocation() {
return location;
}
public Object[] getRawArguments() {
return this.rawArguments;
}
public Object callRealMethod() throws Throwable {
if (method.isAbstract()) {
new Reporter().cannotCallAbstractRealMethod();
}
return realMethod.invoke(mock, rawArguments);
}
public void markVerified() {
this.verified = true;
}
public StubInfo stubInfo() {
return stubInfo;
}
public void markStubbed(StubInfo stubInfo) {
this.stubInfo = stubInfo;
}
public boolean isIgnoredForVerification() {
return isIgnoredForVerification;
}
public void ignoreForVerification() {
isIgnoredForVerification = true;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>) {
Location temp = stubbingInProgress;
stubbingInProgress = null;
reporter.unfinishedStubbing(temp);
}
}
private void validateMostStuff() {
//State is cool when GlobalConfiguration is already loaded
//this cannot really be tested functionally because I cannot dynamically mess up org.mockito.configuration.MockitoConfiguration class
GlobalConfiguration.validate();
if (verificationMode != null) {
Location location = verificationMode.getLocation();
verificationMode = null;
reporter.unfinishedVerificationException(location);
}
getArgumentMatcherStorage().validateState();
}
public void stubbingCompleted(Invocation invocation) {
stubbingInProgress = null;
}
public String toString() {
return "iOngoingStubbing: " + iOngoingStubbing +
", verificationMode: " + verificationMode +
", stubbingInProgress: " + stubbingInProgress;
}
public void reset() {
stubbingInProgress = null;
verificationMode = null;
getArgumentMatcherStorage().reset();
}
public ArgumentMatcherStorage getArgumentMatcherStorage() {
return argumentMatcherStorage;
}
public void mockingStarted(Object mock, Class classToMock) {
if (listener instanceof MockingStartedListener) {
((MockingStartedListener) listener).mockingStarted(mock, classToMock);
}
validateMostStuff();
}
public void setListener(MockingProgressListener listener) {
this.listener = listener;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.progress;
import org.mockito.MockSettings;
import org.mockito.internal.listeners.MockingProgressListener;
import org.mockito.invocation.Invocation;
import org.mockito.verification.VerificationMode;
@SuppressWarnings("unchecked")
public interface MockingProgress {
void reportOngoingStubbing(IOngoingStubbing iOngoingStubbing);
IOngoingStubbing pullOngoingStubbing();
void verificationStarted(VerificationMode verificationMode);
VerificationMode pullVerificationMode();
void stubbingStarted();
void stubbingCompleted(Invocation invocation);
void validateState();
void reset();
/**
* Removes ongoing stubbing so that in case the framework is misused
* state validation errors are more accurate
*/
void resetOngoingStubbing();
ArgumentMatcherStorage getArgumentMatcherStorage();
void mockingStarted(Object mock, Class classToMock);
void setListener(MockingProgressListener listener);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.handler;
import org.mockito.exceptions.Reporter;
import org.mockito.internal.InternalMockHandler;
import org.mockito.internal.listeners.NotifiedMethodInvocationReport;
import org.mockito.internal.stubbing.InvocationContainer;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.MockHandler;
import org.mockito.listeners.InvocationListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.VoidMethodStubbable;
import java.util.List;
/**
* Handler, that call all listeners wanted for this mock, before delegating it
* to the parameterized handler.
*
* Also imposterize MockHandlerImpl, delegate all call of InternalMockHandler to the real mockHandler
*/
class InvocationNotifierHandler<T> implements MockHandler, InternalMockHandler<T> {
private final List<InvocationListener> invocationListeners;
private final InternalMockHandler<T> mockHandler;
public InvocationNotifierHandler(InternalMockHandler<T> mockHandler, MockCreationSettings settings) {
this.mockHandler = mockHandler;
this.invocationListeners = settings.getInvocationListeners();
}
public Object handle(Invocation invocation) throws Throwable {
try {
Object returnedValue = mockHandler.handle(invocation);
notifyMethodCall(invocation, returnedValue);
return returnedValue;
} catch (Throwable t){
notifyMethodCallException(invocation, t);
throw t;
}
}
private void notifyMethodCall(Invocation invocation, Object returnValue) {
for (InvocationListener listener : invocationListeners) {
try {
listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, returnValue));
} catch(Throwable listenerThrowable) {
new Reporter().invocationListenerThrewException(listener, listenerThrowable);
}
}
}
private void notifyMethodCallException(Invocation invocation, Throwable exception) {
for (InvocationListener listener : invocationListeners) {
try {
listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, exception));
} catch(Throwable listenerThrowable) {
new
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
import org.mockito.internal.creation.DelegatingMethod;
import org.mockito.internal.invocation.MockitoMethod;
import java.io.Serializable;
import java.lang.reflect.Method;
public class ObjectMethodsGuru implements Serializable {
private static final long serialVersionUID = -1286718569065470494L;
public boolean isToString(Method method) {
return isToString(new DelegatingMethod(method));
}
public boolean isToString(MockitoMethod method) {
return method.getReturnType() == String.class
&& method.getParameterTypes().length == 0
&& method.getName().equals("toString");
}
public boolean isEqualsMethod(Method method) {
return method.getName().equals("equals")
&& method.getParameterTypes().length == 1
&& method.getParameterTypes()[0] == Object.class;
}
public boolean isHashCodeMethod(Method method) {
return method.getName().equals("hashCode")
&& method.getParameterTypes().length == 0;
}
public boolean isCompareToMethod(Method method) {
return Comparable.class.isAssignableFrom(method.getDeclaringClass())
&& method.getName().equals("compareTo")
&& method.getParameterTypes().length == 1
&& method.getParameterTypes()[0] == method.getDeclaringClass();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.answers;
import java.io.Serializable;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
public class DoesNothing implements Answer<Object>, Serializable {
private static final long serialVersionUID = 4840880517740698416L;
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation.realmethod;
import org.mockito.internal.creation.util.MockitoMethodProxy;
import java.io.Serializable;
public class DefaultRealMethod implements RealMethod, Serializable {
private static final long serialVersionUID = -4596470901191501582L;
private final MockitoMethodProxy methodProxy;
public DefaultRealMethod(MockitoMethodProxy methodProxy) {
this.methodProxy = methodProxy;
}
public Object invoke(Object target, Object[] arguments) throws Throwable {
return methodProxy.invokeSuper(target, arguments);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.SelfDescribing;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.invocation.Location;
import java.io.Serializable;
@SuppressWarnings("unchecked")
public class LocalizedMatcher implements Matcher, ContainsExtraTypeInformation, CapturesArguments, MatcherDecorator, Serializable {
private static final long serialVersionUID = 6748641229659825725L;
private final Matcher actualMatcher;
private final Location location;
public LocalizedMatcher(Matcher actualMatcher) {
this.actualMatcher = actualMatcher;
this.location = new LocationImpl();
}
public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
// yeah right
}
public boolean matches(Object item) {
return actualMatcher.matches(item);
}
public void describeTo(Description description) {
actualMatcher.describeTo(description);
}
public Location getLocation() {
return location;
}
@Override
public String toString() {
return "Localized: " + this.actualMatcher;
}
public SelfDescribing withExtraTypeInfo() {
if (actualMatcher instanceof ContainsExtraTypeInformation) {
return ((ContainsExtraTypeInformation) actualMatcher).withExtraTypeInfo();
} else {
return this;
}
}
public boolean typeMatches(Object object) {
return actualMatcher instanceof ContainsExtraTypeInformation
&& ((ContainsExtraTypeInformation) actualMatcher).typeMatches(object);
}
public void captureFrom(Object argument) {
if (actualMatcher instanceof CapturesArguments) {
((CapturesArguments) actualMatcher).captureFrom(argument);
}
}
//TODO: refactor other 'delegated interfaces' to use the MatcherDecorator feature
public Matcher getActualMatcher() {
return actualMatcher;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> {@code settings.typeToMock} and potentially also {@code settings.extraInterfaces}.</li>
* <li>You may use the information from {@code settings} to create/configure your proxy object.</li>
* <li>Your proxy object should carry the {@code handler} with it. For example, if you generate byte code
* to create the proxy you could generate an extra field to keep the {@code handler} with the generated object.
* Your implementation of {@code MockMaker} is required to provide this instance of {@code handler} when
* {@link #getHandler(Object)} is called.
* </li>
* </ul>
*
* @param settings - mock creation settings like type to mock, extra interfaces and so on.
* @param handler See {@link org.mockito.invocation.MockHandler}.
* <b>Do not</b> provide your own implementation at this time. Make sure your implementation of
* {@link #getHandler(Object)} will return this instance.
* @param <T> Type of the mock to return, actually the <code>settings.getTypeToMock</code>.
* @return The mock instance.
* @since 1.9.5
*/
<T> T createMock(
MockCreationSettings<T> settings,
MockHandler handler
);
/**
* Returns the handler for the {@code mock}. <b>Do not</b> provide your own implementations at this time
* because the work on the {@link MockHandler} api is not completed.
* Use the instance provided to you by Mockito at {@link #createMock} or {@link #resetMock}.
*
* @param mock The mock instance.
* @return may return null - it means that there is no handler attached to provided object.
* This means the passed object is not really a Mockito mock.
* @since 1.9.5
*/
MockHandler getHandler(Object mock);
/**
* Replaces the existing handler on {@code mock} with {@code newHandler}.
*
* <p>The invocation handler actually store invocations to achieve
* stubbing and verification. In order to reset the mock, we pass
* a new instance of the invocation handler.</p>
*
* <p>Your implementation should make sure the
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> {@code newHandler} is correctly associated to passed {@code mock}</p>
*
* @param mock The mock instance whose invocation handler is to be replaced.
* @param newHandler The new invocation handler instance.
* @param settings The mock settings - should you need to access some of the mock creation details.
* @since 1.9.5
*/
void resetMock(
Object mock,
MockHandler newHandler,
MockCreationSettings settings
);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation;
import org.hamcrest.Matcher;
import org.mockito.internal.matchers.MatcherDecorator;
import org.mockito.internal.matchers.VarargMatcher;
import org.mockito.invocation.Invocation;
import java.util.List;
@SuppressWarnings("unchecked")
public class ArgumentsComparator {
public boolean argumentsMatch(InvocationMatcher invocationMatcher, Invocation actual) {
Object[] actualArgs = actual.getArguments();
return argumentsMatch(invocationMatcher, actualArgs) || varArgsMatch(invocationMatcher, actual);
}
public boolean argumentsMatch(InvocationMatcher invocationMatcher, Object[] actualArgs) {
if (actualArgs.length != invocationMatcher.getMatchers().size()) {
return false;
}
for (int i = 0; i < actualArgs.length; i++) {
if (!invocationMatcher.getMatchers().get(i).matches(actualArgs[i])) {
return false;
}
}
return true;
}
//ok, this method is a little bit messy but the vararg business unfortunately is messy...
private boolean varArgsMatch(InvocationMatcher invocationMatcher, Invocation actual) {
if (!actual.getMethod().isVarArgs()) {
//if the method is not vararg forget about it
return false;
}
//we must use raw arguments, not arguments...
Object[] rawArgs = actual.getRawArguments();
List<Matcher> matchers = invocationMatcher.getMatchers();
if (rawArgs.length != matchers.size()) {
return false;
}
for (int i = 0; i < rawArgs.length; i++) {
Matcher m = matchers.get(i);
//it's a vararg because it's the last array in the arg list
if (rawArgs[i] != null && rawArgs[i].getClass().isArray() && i == rawArgs.length-1) {
Matcher actualMatcher;
//this is necessary as the framework often decorates matchers
if (m instanceof MatcherDecorator) {
actualMatcher = ((MatcherDecorator)m).getActualMatcher();
} else {
actualMatcher = m;
}
//this is
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.defaultanswers;
import org.mockito.internal.MockitoCore;
import org.mockito.internal.creation.MockSettingsImpl;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.Serializable;
public class ReturnsMocks implements Answer<Object>, Serializable {
private static final long serialVersionUID = -6755257986994634579L;
private final MockitoCore mockitoCore = new MockitoCore();
private final Answer<Object> delegate = new ReturnsMoreEmptyValues();
public Object answer(InvocationOnMock invocation) throws Throwable {
Object ret = delegate.answer(invocation);
if (ret != null) {
return ret;
}
return returnValueFor(invocation.getMethod().getReturnType());
}
Object returnValueFor(Class<?> clazz) {
if (!mockitoCore.isTypeMockable(clazz)) {
return null;
}
return mockitoCore.mock(clazz, new MockSettingsImpl().defaultAnswer(this));
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import org.mockito.*;
import org.mockito.configuration.AnnotationEngine;
import org.mockito.exceptions.Reporter;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.util.MockUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import static org.mockito.Mockito.withSettings;
/**
* Process fields annotated with @Spy.
* <p/>
* <p>
* Will try transform the field in a spy as with <code>Mockito.spy()</code>.
* </p>
* <p/>
* <p>
* If the field is not initialized, will try to initialize it, with a no-arg constructor.
* </p>
* <p/>
* <p>
* If the field is also annotated with the <strong>compatible</strong> @InjectMocks then the field will be ignored,
* The injection engine will handle this specific case.
* </p>
* <p/>
* <p>This engine will fail, if the field is also annotated with incompatible Mockito annotations.
*/
@SuppressWarnings({"unchecked"})
public class SpyAnnotationEngine implements AnnotationEngine {
public Object createMockFor(Annotation annotation, Field field) {
return null;
}
@SuppressWarnings("deprecation") // for MockitoAnnotations.Mock
public void process(Class<?> context, Object testInstance) {
Field[] fields = context.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Spy.class) && !field.isAnnotationPresent(InjectMocks.class)) {
assertNoIncompatibleAnnotations(Spy.class, field, Mock.class, org.mockito.MockitoAnnotations.Mock.class, Captor.class);
field.setAccessible(true);
Object instance;
try {
instance = field.get(testInstance);
assertNotInterface(instance, field.getType());
if (new
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> MockUtil().isMock(instance)) {
// instance has been spied earlier
// for example happens when MockitoAnnotations.initMocks is called two times.
Mockito.reset(instance);
} else if (instance != null) {
field.set(testInstance, Mockito.mock(instance.getClass(), withSettings()
.spiedInstance(instance)
.defaultAnswer(Mockito.CALLS_REAL_METHODS)
.name(field.getName())));
} else {
field.set(testInstance, newSpyInstance(testInstance, field));
}
} catch (Exception e) {
throw new MockitoException("Unable to initialize @Spy annotated field '" + field.getName() + "'.\n" + e.getMessage(), e);
}
}
}
}
private static void assertNotInterface(Object testInstance, Class<?> type) {
type = testInstance != null? testInstance.getClass() : type;
if (type.isInterface()) {
throw new MockitoException("Type '" + type.getSimpleName() + "' is an interface and it cannot be spied on.");
}
}
private static Object newSpyInstance(Object testInstance, Field field)
throws InstantiationException, IllegalAccessException, InvocationTargetException {
MockSettings settings = withSettings()
.defaultAnswer(Mockito.CALLS_REAL_METHODS)
.name(field.getName());
Class<?> type = field.getType();
if (type.isInterface()) {
return Mockito.mock(type, settings.useConstructor());
}
if (!Modifier.isStatic(type.getModifiers())) {
Class<?> enclosing = type.getEnclosingClass();
if (enclosing != null) {
if (!enclosing.isInstance(testInstance)) {
throw new MockitoException("@Spy annotation can only initialize inner classes declared in the test. "
+ "Inner class: '" + type.getSimpleName() + "', "
+ "outer class: '" + enclosing.getSimpleName() + "'.");
}
return Mockito.mock(type, settings
.useConstructor()
.outerInstance(testInstance));
}
}
Constructor<?> constructor;
try {
constructor = type.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
throw new MockitoException("Please ensure that the type '" + type.getSimpleName() + "'
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>static inner abstract class:
* InnerAbstract spy = mock(InnerAbstract.class, withSettings()
* .useConstructor().outerInstance(outerInstance).defaultAnswer(CALLS_REAL_METHODS));
* </code></pre>
*
* For more information please see {@link MockSettings#useConstructor()}.
*/
@SuppressWarnings("unchecked")
public class Mockito extends Matchers {
static final MockitoCore MOCKITO_CORE = new MockitoCore();
/**
* The default <code>Answer</code> of every mock <b>if</b> the mock was not stubbed.
* Typically it just returns some empty value.
* <p>
* {@link Answer} can be used to define the return values of unstubbed invocations.
* <p>
* This implementation first tries the global configuration.
* If there is no global configuration then it uses {@link ReturnsEmptyValues} (returns zeros, empty collections, nulls, etc.)
*/
public static final Answer<Object> RETURNS_DEFAULTS = Answers.RETURNS_DEFAULTS.get();
/**
* Optional <code>Answer</code> to be used with {@link Mockito#mock(Class, Answer)}.
* <p>
* {@link Answer} can be used to define the return values of unstubbed invocations.
* <p>
* This implementation can be helpful when working with legacy code.
* Unstubbed methods often return null. If your code uses the object returned by an unstubbed call you get a NullPointerException.
* This implementation of Answer <b>returns SmartNull instead of null</b>.
* <code>SmartNull</code> gives nicer exception message than NPE because it points out the line where unstubbed method was called. You just click on the stack trace.
* <p>
* <code>ReturnsSmartNulls</code> first tries to return ordinary return values (see {@link ReturnsMoreEmptyValues})
* then it tries to return SmartNull. If the return type is final then plain null is returned.
* <p>
* <code>ReturnsSmartNulls</code> will be probably the default return values strategy in Mockito 2.0.
* <p>
* Example:
* <pre class="code"><code class="java">
* Foo mock =
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> (Foo.class, RETURNS_SMART_NULLS);
*
* //calling unstubbed method here:
* Stuff stuff = mock.getStuff();
*
* //using object returned by unstubbed call:
* stuff.doSomething();
*
* //Above doesn't yield NullPointerException this time!
* //Instead, SmartNullPointerException is thrown.
* //Exception's cause links to unstubbed <i>mock.getStuff()</i> - just click on the stack trace.
* </code></pre>
*/
public static final Answer<Object> RETURNS_SMART_NULLS = Answers.RETURNS_SMART_NULLS.get();
/**
* Optional <code>Answer</code> to be used with {@link Mockito#mock(Class, Answer)}
* <p>
* {@link Answer} can be used to define the return values of unstubbed invocations.
* <p>
* This implementation can be helpful when working with legacy code.
* <p>
* ReturnsMocks first tries to return ordinary return values (see {@link ReturnsMoreEmptyValues})
* then it tries to return mocks. If the return type cannot be mocked (e.g. is final) then plain null is returned.
* <p>
*/
public static final Answer<Object> RETURNS_MOCKS = Answers.RETURNS_MOCKS.get();
/**
* Optional <code>Answer</code> to be used with {@link Mockito#mock(Class, Answer)}.
* <p>
* Example that shows how deep stub works:
* <pre class="code"><code class="java">
* Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);
*
* // note that we're stubbing a chain of methods here: getBar().getName()
* when(mock.getBar().getName()).thenReturn("deep");
*
* // note that we're chaining method calls: getBar().getName()
* assertEquals("deep", mock.getBar().getName());
* </code></pre>
* </p>
*
* <p>
* <strong>WARNING: </strong>
* This feature should rarely be required for regular clean code! Leave it for legacy
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>(
* person.getAddress("the docks").getStreet(),
* person.getAddress("the docks").getStreet(Locale.CHINESE),
* person.getAddress("the docks").getStreet(Locale.ITALIAN)
* );
* inOrder.verify(person.getAddress("the docks").getStreet(), times(1)).getName();
* inOrder.verify(person.getAddress("the docks").getStreet()).getLongName();
* inOrder.verify(person.getAddress("the docks").getStreet(Locale.ITALIAN), atLeast(1)).getName();
* inOrder.verify(person.getAddress("the docks").getStreet(Locale.CHINESE)).getName();
* </code></pre>
* </p>
*
* <p>
* How deep stub work internally?
* <pre class="code"><code class="java">
* //this:
* Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);
* when(mock.getBar().getName(), "deep");
*
* //is equivalent of
* Foo foo = mock(Foo.class);
* Bar bar = mock(Bar.class);
* when(foo.getBar()).thenReturn(bar);
* when(bar.getName()).thenReturn("deep");
* </code></pre>
* </p>
*
* <p>
* This feature will not work when any return type of methods included in the chain cannot be mocked
* (for example: is a primitive or a final class). This is because of java type system.
* </p>
*/
public static final Answer<Object> RETURNS_DEEP_STUBS = Answers.RETURNS_DEEP_STUBS.get();
/**
* Optional <code>Answer</code> to be used with {@link Mockito#mock(Class, Answer)}
* <p>
* {@link Answer} can be used to define the return values of unstubbed invocations.
* <p>
* This implementation can be helpful when working with legacy code.
* When this implementation is used, unstubbed methods will delegate to the real implementation.
* This is a way to create a partial mock
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> object that calls real methods by default.
* <p>
* As usual you are going to read <b>the partial mock warning</b>:
* Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy objects.
* How does partial mock fit into this paradigm? Well, it just doesn't...
* Partial mock usually means that the complexity has been moved to a different method on the same object.
* In most cases, this is not the way you want to design your application.
* <p>
* However, there are rare cases when partial mocks come handy:
* dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.)
* However, I wouldn't use partial mocks for new, test-driven & well-designed code.
* <p>
* Example:
* <pre class="code"><code class="java">
* Foo mock = mock(Foo.class, CALLS_REAL_METHODS);
*
* // this calls the real implementation of Foo.getSomething()
* value = mock.getSomething();
*
* when(mock.getSomething()).thenReturn(fakeValue);
*
* // now fakeValue is returned
* value = mock.getSomething();
* </code></pre>
*/
public static final Answer<Object> CALLS_REAL_METHODS = Answers.CALLS_REAL_METHODS.get();
/**
* Creates mock object of given class or interface.
* <p>
* See examples in javadoc for {@link Mockito} class
*
* @param classToMock class or interface to mock
* @return mock object
*/
public static <T> T mock(Class<T> classToMock) {
return mock(classToMock, withSettings().defaultAnswer(RETURNS_DEFAULTS));
}
/**
* Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors.
* <p>
* Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators.
* <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks.
* <p>
*
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> <b>If you use <code>@Mock</code> annotation then you've got naming mocks for free!</b> <code>@Mock</code> uses field name as mock name. {@link Mock Read more.}
* <p>
*
* See examples in javadoc for {@link Mockito} class
*
* @param classToMock class or interface to mock
* @param name of the mock
* @return mock object
*/
public static <T> T mock(Class<T> classToMock, String name) {
return mock(classToMock, withSettings()
.name(name)
.defaultAnswer(RETURNS_DEFAULTS));
}
/**
* Returns a MockingDetails instance that enables inspecting a particular object for Mockito related information.
* Can be used to find out if given object is a Mockito mock
* or to find out if a given mock is a spy or mock.
* <p>
* In future Mockito versions MockingDetails may grow and provide other useful information about the mock,
* e.g. invocations, stubbing info, etc.
*
* @param toInspect - object to inspect. null input is allowed.
* @return A {@link org.mockito.MockingDetails} instance.
* @since 1.9.5
*/
public static MockingDetails mockingDetails(Object toInspect) {
return MOCKITO_CORE.mockingDetails(toInspect);
}
/**
* <b>Deprecated : Please use mock(Foo.class, defaultAnswer);</b>
* <p>
* See {@link Mockito#mock(Class, Answer)}
* <p>
* Why it is deprecated? ReturnValues is being replaced by Answer
* for better consistency & interoperability of the framework.
* Answer interface has been in Mockito for a while and it has the same responsibility as ReturnValues.
* There's no point in mainting exactly the same interfaces.
* <p>
* Creates mock with a specified strategy for its return values.
* It's quite advanced feature and typically you don't need it to write decent tests.
* However it can be helpful when working with legacy systems.
* <p>
* Obviously return values are used only when you don't stub the method call.
*
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>
* @param mocks to be verified
*/
public static void verifyNoMoreInteractions(Object... mocks) {
MOCKITO_CORE.verifyNoMoreInteractions(mocks);
}
/**
* Verifies that no interactions happened on given mocks.
* <pre class="code"><code class="java">
* verifyZeroInteractions(mockOne, mockTwo);
* </code></pre>
* This method will also detect invocations
* that occurred before the test method, for example: in <code>setUp()</code>, <code>@Before</code> method or in constructor.
* Consider writing nice code that makes interactions only in test methods.
* <p>
* See also {@link Mockito#never()} - it is more explicit and communicates the intent well.
* <p>
* See examples in javadoc for {@link Mockito} class
*
* @param mocks to be verified
*/
public static void verifyZeroInteractions(Object... mocks) {
MOCKITO_CORE.verifyNoMoreInteractions(mocks);
}
/**
* <pre class="code"><code class="java">
* //Instead of:
* stubVoid(mock).toThrow(e).on().someVoidMethod();
*
* //Please do:
* doThrow(e).when(mock).someVoidMethod();
* </code></pre>
*
* doThrow() replaces stubVoid() because of improved readability and consistency with the family of doAnswer() methods.
* <p>
* Originally, <code>stubVoid()</code> was used for stubbing void methods with exceptions. E.g:
*
* <pre class="code"><code class="java">
* stubVoid(mock).toThrow(new RuntimeException()).on().someMethod();
*
* //you can stub with different behavior for consecutive calls.
* //Last stubbing (e.g. toReturn()) determines the behavior for further consecutive calls.
* stubVoid(mock)
* .toThrow(new RuntimeException())
* .toReturn()
* .on().someMethod();
* </code></pre>
*
* See examples in javadoc for {@link Mockito} class
*
* @deprecated Use {@link Mockito#doThrow(Throwable)} method for stubbing voids
*
*
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> that {@link Mockito#when(Object)} is always recommended for stubbing because it is argument type-safe
* and more readable</b> (especially when stubbing consecutive calls).
* <p>
* Here are those rare occasions when doReturn() comes handy:
* <p>
*
* <ol>
* <li>When spying real objects and calling real methods on a spy brings side effects
*
* <pre class="code"><code class="java">
* List list = new LinkedList();
* List spy = spy(list);
*
* //Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty)
* when(spy.get(0)).thenReturn("foo");
*
* //You have to use doReturn() for stubbing:
* doReturn("foo").when(spy).get(0);
* </code></pre>
* </li>
*
* <li>Overriding a previous exception-stubbing:
* <pre class="code"><code class="java">
* when(mock.foo()).thenThrow(new RuntimeException());
*
* //Impossible: the exception-stubbed foo() method is called so RuntimeException is thrown.
* when(mock.foo()).thenReturn("bar");
*
* //You have to use doReturn() for stubbing:
* doReturn("bar").when(mock).foo();
* </code></pre>
* </li>
* </ol>
*
* Above scenarios shows a tradeoff of Mockito's elegant syntax. Note that the scenarios are very rare, though.
* Spying should be sporadic and overriding exception-stubbing is very rare. Not to mention that in general
* overridding stubbing is a potential code smell that points out too much stubbing.
* <p>
* See examples in javadoc for {@link Mockito} class
*
* @param toBeReturned to be returned when the stubbed method is called
* @return stubber - to select a method for stubbing
*/
public static Stubber doReturn(Object toBeReturned) {
return MOCKITO_CORE.doAnswer(new Returns(toBeReturned));
}
/**
*
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> Creates {@link org.mockito.InOrder} object that allows verifying mocks in order.
*
* <pre class="code"><code class="java">
* InOrder inOrder = inOrder(firstMock, secondMock);
*
* inOrder.verify(firstMock).add("was called first");
* inOrder.verify(secondMock).add("was called second");
* </code></pre>
*
* Verification in order is flexible - <b>you don't have to verify all interactions</b> one-by-one
* but only those that you are interested in testing in order.
* <p>
* Also, you can create InOrder object passing only mocks that are relevant for in-order verification.
* <p>
* <code>InOrder</code> verification is 'greedy'. You will hardly every notice it but
* if you want to find out more search for 'greedy' on the Mockito
* <a href="http://code.google.com/p/mockito/w/list">wiki pages</a>.
* <p>
* As of Mockito 1.8.4 you can verifyNoMoreInvocations() in order-sensitive way. Read more: {@link InOrder#verifyNoMoreInteractions()}
* <p>
* See examples in javadoc for {@link Mockito} class
*
* @param mocks to be verified in order
*
* @return InOrder object to be used to verify in order
*/
public static InOrder inOrder(Object... mocks) {
return MOCKITO_CORE.inOrder(mocks);
}
/**
* Ignores stubbed methods of given mocks for the sake of verification.
* Sometimes useful when coupled with <code>verifyNoMoreInteractions()</code> or verification <code>inOrder()</code>.
* Helps avoiding redundant verification of stubbed calls - typically we're not interested in verifying stubs.
* <p>
* <b>Warning</b>, <code>ignoreStubs()</code> might lead to overuse of <code>verifyNoMoreInteractions(ignoreStubs(...));</code>
* Bear in mind that Mockito does not recommend bombarding every test with <code>verifyNoMoreInteractions()</code>
* for the reasons outlined in javadoc for {@
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>java">
* List list = mock(List.class);
* when(mock.get(0)).thenReturn("foo");
*
* list.add(0);
* System.out.println(list.get(0)); //we don't want to verify this
* list.clear();
*
* InOrder inOrder = inOrder(ignoreStubs(list));
* inOrder.verify(list).add(0);
* inOrder.verify(list).clear();
* inOrder.verifyNoMoreInteractions();
* </code></pre>
*
* @since 1.9.0
* @param mocks input mocks that will be changed
* @return the same mocks that were passed in as parameters
*/
public static Object[] ignoreStubs(Object... mocks) {
return MOCKITO_CORE.ignoreStubs(mocks);
}
/**
* Allows verifying exact number of invocations. E.g:
* <pre class="code"><code class="java">
* verify(mock, times(2)).someMethod("some arg");
* </code></pre>
*
* See examples in javadoc for {@link Mockito} class
*
* @param wantedNumberOfInvocations wanted number of invocations
*
* @return verification mode
*/
public static VerificationMode times(int wantedNumberOfInvocations) {
return VerificationModeFactory.times(wantedNumberOfInvocations);
}
/**
* Alias to <code>times(0)</code>, see {@link Mockito#times(int)}
* <p>
* Verifies that interaction did not happen. E.g:
* <pre class="code"><code class="java">
* verify(mock, never()).someMethod();
* </code></pre>
*
* <p>
* If you want to verify there were NO interactions with the mock
* check out {@link Mockito#verifyZeroInteractions(Object...)}
* or {@link Mockito#verifyNoMoreInteractions(Object...)}
* <p>
* See examples in javadoc for {@link Mockito} class
*
* @return verification mode
*/
public static VerificationMode never() {
return times(0);
}
/**
* Allows at-least-once verification. E.g:
*
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection;
import org.mockito.exceptions.Reporter;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.util.reflection.FieldInitializationReport;
import org.mockito.internal.util.reflection.FieldInitializer;
import org.mockito.internal.util.reflection.FieldInitializer.ConstructorArgumentResolver;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Injection strategy based on constructor.
*
* <p>
* The strategy will search for the constructor with most parameters
* and try to resolve mocks by type.
* </p>
*
* <blockquote>
* TODO on missing mock type, shall it abandon or create "noname" mocks.
* TODO and what if the arg type is not mockable.
* </blockquote>
*
* <p>
* For now the algorithm tries to create anonymous mocks if an argument type is missing.
* If not possible the algorithm abandon resolution.
* </p>
*/
public class ConstructorInjection extends MockInjectionStrategy {
private ConstructorArgumentResolver argResolver;
public ConstructorInjection() { }
// visible for testing
ConstructorInjection(ConstructorArgumentResolver argResolver) {
this.argResolver = argResolver;
}
public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
try {
SimpleArgumentResolver simpleArgumentResolver = new SimpleArgumentResolver(mockCandidates);
FieldInitializationReport report = new FieldInitializer(fieldOwner, field, simpleArgumentResolver).initialize();
return report.fieldWasInitializedUsingContructorArgs();
} catch (MockitoException e) {
if(e.getCause() instanceof InvocationTargetException) {
Throwable realCause = e.getCause().getCause();
new Reporter().fieldInitialisationThrewException(field, realCause);
}
// other causes should be fine
return false;
}
}
/**
* Returns mocks that match the argument type, if not possible assigns null.
*/
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> static class SimpleArgumentResolver implements ConstructorArgumentResolver {
final Set<Object> objects;
public SimpleArgumentResolver(Set<Object> objects) {
this.objects = objects;
}
public Object[] resolveTypeInstances(Class<?>... argTypes) {
List<Object> argumentInstances = new ArrayList<Object>(argTypes.length);
for (Class<?> argType : argTypes) {
argumentInstances.add(objectThatIsAssignableFrom(argType));
}
return argumentInstances.toArray();
}
private Object objectThatIsAssignableFrom(Class<?> argType) {
for (Object object : objects) {
if(argType.isAssignableFrom(object.getClass())) return object;
}
return null;
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static org.mockito.internal.util.Checks.checkItemsNotNull;
import static org.mockito.internal.util.Checks.checkNotNull;
import static org.mockito.internal.util.collections.Sets.newMockSafeHashSet;
/**
* Internal injection configuration utility.
*
* <p>
* Allow the user of this class to configure the way the injection of mocks will happen.
* </p>
*
*/
public class MockInjection {
/**
* Create a new configuration setup for a field
*
*
* @param field Field needing mock injection
* @param ofInstance Instance owning the <code>field</code>
* @return New configuration builder
*/
public static OngoingMockInjection onField(Field field, Object ofInstance) {
return new OngoingMockInjection(field, ofInstance);
}
/**
* Create a new configuration setup for fields
*
*
* @param fields Fields needing mock injection
* @param ofInstance Instance owning the <code>field</code>
* @return New configuration builder
*/
public static OngoingMockInjection onFields(Set<Field> fields, Object ofInstance) {
return new OngoingMockInjection(fields, ofInstance);
}
/**
* Ongoing configuration of the mock injector.
*/
public static class OngoingMockInjection {
private final Set<Field> fields = new HashSet<Field>();
private final Set<Object> mocks = newMockSafeHashSet();
private final Object fieldOwner;
private final MockInjectionStrategy injectionStrategies = MockInjectionStrategy.nop();
private final MockInjectionStrategy postInjectionStrategies = MockInjectionStrategy.nop();
private OngoingMockInjection(Field field, Object fieldOwner) {
this(Collections.singleton(field), fieldOwner);
}
private OngoingMockInjection(Set<Field> fields, Object fieldOwner) {
this.fieldOwner = checkNotNull(fieldOwner, "fieldOwner
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>");
this.fields.addAll(checkItemsNotNull(fields, "fields"));
}
public OngoingMockInjection withMocks(Set<Object> mocks) {
this.mocks.addAll(checkNotNull(mocks, "mocks"));
return this;
}
public OngoingMockInjection tryConstructorInjection() {
injectionStrategies.thenTry(new ConstructorInjection());
return this;
}
public OngoingMockInjection tryPropertyOrFieldInjection() {
injectionStrategies.thenTry(new PropertyAndSetterInjection());
return this;
}
public OngoingMockInjection handleSpyAnnotation() {
postInjectionStrategies.thenTry(new SpyOnInjectedFieldsHandler());
return this;
}
public void apply() {
for (Field field : fields) {
injectionStrategies.process(field, fieldOwner, mocks);
postInjectionStrategies.process(field, fieldOwner, mocks);
}
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.invocation;
import java.io.Serializable;
/**
* Mockito handler of an invocation on a mock. This is a core part of the API, the heart of Mockito.
* See also the {@link org.mockito.plugins.MockMaker}.
* <p>
* This api is work in progress. Do not provide your own implementations.
* Mockito will provide you with the implementation via other {@link org.mockito.plugins.MockMaker} methods.
*/
public interface MockHandler extends Serializable {
/**
* Takes an invocation object and handles it.
* <p>
* The default implementation provided by Mockito handles invocations by recording
* method calls on mocks for further verification, captures the stubbing information when mock is stubbed,
* returns the stubbed values for invocations that have been stubbed, and much more.
*
* @param invocation The invocation to handle
* @return Result
* @throws Throwable Throwable
*/
Object handle(Invocation invocation) throws Throwable;
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.mock;
import org.mockito.Incubating;
import org.mockito.listeners.InvocationListener;
import org.mockito.stubbing.Answer;
import java.util.List;
import java.util.Set;
/**
* Informs about the mock settings. An immutable view of {@link org.mockito.MockSettings}.
*/
public interface MockCreationSettings<T> {
/**
* Mocked type. An interface or class the mock should implement / extend.
*/
Class<T> getTypeToMock();
/**
* the extra interfaces the mock object should implement.
*/
Set<Class> getExtraInterfaces();
/**
* the name of this mock, as printed on verification errors; see {@link org.mockito.MockSettings#name}.
*/
MockName getMockName();
/**
* the default answer for this mock, see {@link org.mockito.MockSettings#defaultAnswer}.
*/
Answer getDefaultAnswer();
/**
* the spied instance - needed for spies.
*/
Object getSpiedInstance();
/**
* if the mock is serializable, see {@link org.mockito.MockSettings#serializable}.
*/
boolean isSerializable();
/**
* @return the serializable mode of this mock
*/
SerializableMode getSerializableMode();
/**
* Whether the mock is only for stubbing, i.e. does not remember
* parameters on its invocation and therefore cannot
* be used for verification
*/
boolean isStubOnly();
/**
* The invocation listeners attached to this mock, see {@link org.mockito.MockSettings#invocationListeners}.
*/
List<InvocationListener> getInvocationListeners();
/**
* Informs whether the mock instance should be created via constructor
*
* @since 1.10.12
*/
@Incubating
boolean isUsingConstructor();
/**
* Used when mocking non-static inner classes in conjunction with {@link #isUsingConstructor()}
*
* @return the outer class instance used for creation of the mock object via the constructor.
* @since 1.10.12
*/
@Incubating
Object getOuterClass
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.util;
//TODO SF Replace with RealMethod and get rid of (possibly).
public interface MockitoMethodProxy {
Object invokeSuper(Object target, Object[] arguments) throws Throwable;
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.answers;
import org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter;
import org.mockito.internal.util.MockUtil;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.Serializable;
public class ThrowsException implements Answer<Object>, Serializable {
private static final long serialVersionUID = 1128820328555183980L;
private final Throwable throwable;
private final ConditionalStackTraceFilter filter = new ConditionalStackTraceFilter();
public ThrowsException(Throwable throwable) {
this.throwable = throwable;
}
public Object answer(InvocationOnMock invocation) throws Throwable {
if (new MockUtil().isMock(throwable)) {
throw throwable;
}
Throwable t = throwable.fillInStackTrace();
filter.filter(t);
throw t;
}
public Throwable getThrowable() {
return throwable;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.configuration.AnnotationEngine;
import org.mockito.exceptions.Reporter;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.util.reflection.FieldSetter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* Initializes fields annotated with @{@link org.mockito.Mock} or @{@link org.mockito.Captor}.
*
* <p>
* The {@link #process(Class, Object)} method implementation <strong>does not</strong> process super classes!
*
* @see MockitoAnnotations
*/
@SuppressWarnings("unchecked")
public class DefaultAnnotationEngine implements AnnotationEngine {
private final Map<Class<? extends Annotation>, FieldAnnotationProcessor<?>> annotationProcessorMap = new HashMap<Class<? extends Annotation>, FieldAnnotationProcessor<?>>();
public DefaultAnnotationEngine() {
registerAnnotationProcessor(Mock.class, new MockAnnotationProcessor());
registerAnnotationProcessor(MockitoAnnotations.Mock.class, new MockitoAnnotationsMockAnnotationProcessor());
registerAnnotationProcessor(Captor.class, new CaptorAnnotationProcessor());
}
/* (non-Javadoc)
* @see org.mockito.AnnotationEngine#createMockFor(java.lang.annotation.Annotation, java.lang.reflect.Field)
*/
@SuppressWarnings("deprecation")
public Object createMockFor(Annotation annotation, Field field) {
return forAnnotation(annotation).process(annotation, field);
}
private <A extends Annotation> FieldAnnotationProcessor<A> forAnnotation(A annotation) {
if (annotationProcessorMap.containsKey(annotation.annotationType())) {
return (FieldAnnotationProcessor<A>) annotationProcessorMap.get(annotation.annotationType());
}
return new FieldAnnotationProcessor<A>() {
public Object process(A annotation, Field field) {
return null;
}
};
}
private <A extends Annotation> void registerAnnotationProcessor(Class<
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>A> annotationClass, FieldAnnotationProcessor<A> fieldAnnotationProcessor) {
annotationProcessorMap.put(annotationClass, fieldAnnotationProcessor);
}
public void process(Class<?> clazz, Object testInstance) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
boolean alreadyAssigned = false;
for(Annotation annotation : field.getAnnotations()) {
Object mock = createMockFor(annotation, field);
if (mock != null) {
throwIfAlreadyAssigned(field, alreadyAssigned);
alreadyAssigned = true;
try {
new FieldSetter(testInstance, field).set(mock);
} catch (Exception e) {
throw new MockitoException("Problems setting field " + field.getName() + " annotated with "
+ annotation, e);
}
}
}
}
}
void throwIfAlreadyAssigned(Field field, boolean alreadyAssigned) {
if (alreadyAssigned) {
new Reporter().moreThanOneAnnotationNotAllowed(field.getName());
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection.filter;
import org.mockito.exceptions.Reporter;
import org.mockito.internal.util.reflection.BeanPropertySetter;
import org.mockito.internal.util.reflection.FieldSetter;
import java.lang.reflect.Field;
import java.util.Collection;
/**
* This node returns an actual injecter which will be either :
*
* <ul>
* <li>an {@link OngoingInjecter} that do nothing if a candidate couldn't be found</li>
* <li>an {@link OngoingInjecter} that will try to inject the candidate trying first the property setter then if not possible try the field access</li>
* </ul>
*/
public class FinalMockCandidateFilter implements MockCandidateFilter {
public OngoingInjecter filterCandidate(final Collection<Object> mocks, final Field field, final Object fieldInstance) {
if(mocks.size() == 1) {
final Object matchingMock = mocks.iterator().next();
return new OngoingInjecter() {
public Object thenInject() {
try {
if (!new BeanPropertySetter(fieldInstance, field).set(matchingMock)) {
new FieldSetter(fieldInstance, field).set(matchingMock);
}
} catch (RuntimeException e) {
new Reporter().cannotInjectDependency(field, matchingMock, e);
}
return matchingMock;
}
};
}
return new OngoingInjecter() {
public Object thenInject() {
return null;
}
};
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.handler;
import org.mockito.internal.InternalMockHandler;
import org.mockito.internal.progress.HandyReturnValues;
import org.mockito.internal.stubbing.InvocationContainer;
import org.mockito.invocation.Invocation;
import org.mockito.mock.MockCreationSettings;
import org.mockito.stubbing.VoidMethodStubbable;
import java.util.List;
/**
* Protects the results from delegate MockHandler. Makes sure the results are valid.
*
* by Szczepan Faber, created at: 5/22/12
*/
class NullResultGuardian implements InternalMockHandler {
private final InternalMockHandler delegate;
public NullResultGuardian(InternalMockHandler delegate) {
this.delegate = delegate;
}
public Object handle(Invocation invocation) throws Throwable {
Object result = delegate.handle(invocation);
Class<?> returnType = invocation.getMethod().getReturnType();
if(result == null && returnType.isPrimitive()) {
//primitive values cannot be null
return new HandyReturnValues().returnFor(returnType);
} else {
return result;
}
}
//boring delegation:
public MockCreationSettings getMockSettings() {
return delegate.getMockSettings();
}
public VoidMethodStubbable voidMethodStubbable(Object mock) {
return delegate.voidMethodStubbable(mock);
}
public void setAnswersForStubbing(List answers) {
delegate.setAnswersForStubbing(answers);
}
public InvocationContainer getInvocationContainer() {
return delegate.getInvocationContainer();
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util.collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import static java.util.Arrays.asList;
public abstract class Sets {
public static Set<Object> newMockSafeHashSet(Iterable<Object> mocks) {
return HashCodeAndEqualsSafeSet.of(mocks);
}
public static Set<Object> newMockSafeHashSet(Object... mocks) {
return HashCodeAndEqualsSafeSet.of(mocks);
}
public static IdentitySet newIdentitySet() {
return new IdentitySet();
}
public static <T> Set<T> newSet(T ... elements) {
if (elements == null) {
throw new IllegalArgumentException("Expected an array of elements (or empty array) but received a null.");
}
return new LinkedHashSet<T>(asList(elements));
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util.reflection;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.util.MockUtil;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Initialize a field with type instance if a default constructor can be found.
*
* <p>
* If the given field is already initialized, then <strong>the actual instance is returned</strong>.
* This initializer doesn't work with inner classes, local classes, interfaces or abstract types.
* </p>
*
*/
public class FieldInitializer {
private final Object fieldOwner;
private final Field field;
private final ConstructorInstantiator instantiator;
/**
* Prepare initializer with the given field on the given instance.
*
* <p>
* This constructor fail fast if the field type cannot be handled.
* </p>
*
* @param fieldOwner Instance of the test.
* @param field Field to be initialize.
*/
public FieldInitializer(Object fieldOwner, Field field) {
this(fieldOwner, field, new NoArgConstructorInstantiator(fieldOwner, field));
}
/**
* Prepare initializer with the given field on the given instance.
*
* <p>
* This constructor fail fast if the field type cannot be handled.
* </p>
*
* @param fieldOwner Instance of the test.
* @param field Field to be initialize.
* @param argResolver Constructor parameters resolver
*/
public FieldInitializer(Object fieldOwner, Field field, ConstructorArgumentResolver argResolver) {
this(fieldOwner, field, new ParameterizedConstructorInstantiator(fieldOwner, field, argResolver));
}
private FieldInitializer(Object fieldOwner, Field field, ConstructorInstantiator instantiator) {
if(new FieldReader(fieldOwner, field).isNull()) {
checkNotLocal(field);
checkNot
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>Inner(field);
checkNotInterface(field);
checkNotAbstract(field);
}
this.fieldOwner = fieldOwner;
this.field = field;
this.instantiator = instantiator;
}
/**
* Initialize field if not initialized and return the actual instance.
*
* @return Actual field instance.
*/
public FieldInitializationReport initialize() {
final AccessibilityChanger changer = new AccessibilityChanger();
changer.enableAccess(field);
try {
return acquireFieldInstance();
} catch(IllegalAccessException e) {
throw new MockitoException("Problems initializing field '" + field.getName() + "' of type '" + field.getType().getSimpleName() + "'", e);
} finally {
changer.safelyDisableAccess(field);
}
}
private void checkNotLocal(Field field) {
if(field.getType().isLocalClass()) {
throw new MockitoException("the type '" + field.getType().getSimpleName() + "' is a local class.");
}
}
private void checkNotInner(Field field) {
if(field.getType().isMemberClass() && !Modifier.isStatic(field.getType().getModifiers())) {
throw new MockitoException("the type '" + field.getType().getSimpleName() + "' is an inner class.");
}
}
private void checkNotInterface(Field field) {
if(field.getType().isInterface()) {
throw new MockitoException("the type '" + field.getType().getSimpleName() + "' is an interface.");
}
}
private void checkNotAbstract(Field field) {
if(Modifier.isAbstract(field.getType().getModifiers())) {
throw new MockitoException("the type '" + field.getType().getSimpleName() + " is an abstract class.");
}
}
private FieldInitializationReport acquireFieldInstance() throws IllegalAccessException {
Object fieldInstance = field.get(fieldOwner);
if(fieldInstance != null) {
return new FieldInitializationReport(fieldInstance, false, false);
}
return instantiator.instantiate();
}
/**
* Represents the strategy used to resolve actual instances
* to be given to a constructor given the argument types.
*/
public interface ConstructorArgumentResolver {
/**
* Try to resolve instances from types.
*
* <p>
* Checks on the real argument type or
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> on the correct argument number
* will happen during the field initialization {@link FieldInitializer#initialize()}.
* I.e the only responsibility of this method, is to provide instances <strong>if possible</strong>.
* </p>
*
* @param argTypes Constructor argument types, should not be null.
* @return The argument instances to be given to the constructor, should not be null.
*/
Object[] resolveTypeInstances(Class<?>... argTypes);
}
private interface ConstructorInstantiator {
FieldInitializationReport instantiate();
}
/**
* Constructor instantiating strategy for no-arg constructor.
*
* <p>
* If a no-arg constructor can be found then the instance is created using
* this constructor.
* Otherwise a technical MockitoException is thrown.
* </p>
*/
static class NoArgConstructorInstantiator implements ConstructorInstantiator {
private final Object testClass;
private final Field field;
/**
* Internal, checks are done by FieldInitializer.
* Fields are assumed to be accessible.
*/
NoArgConstructorInstantiator(Object testClass, Field field) {
this.testClass = testClass;
this.field = field;
}
public FieldInitializationReport instantiate() {
final AccessibilityChanger changer = new AccessibilityChanger();
Constructor<?> constructor = null;
try {
constructor = field.getType().getDeclaredConstructor();
changer.enableAccess(constructor);
final Object[] noArg = new Object[0];
Object newFieldInstance = constructor.newInstance(noArg);
new FieldSetter(testClass, field).set(newFieldInstance);
return new FieldInitializationReport(field.get(testClass), true, false);
} catch (NoSuchMethodException e) {
throw new MockitoException("the type '" + field.getType().getSimpleName() + "' has no default constructor", e);
} catch (InvocationTargetException e) {
throw new MockitoException("the default constructor of type '" + field.getType().getSimpleName() + "' has raised an exception (see the stack trace for cause): " + e.getTargetException().toString(), e);
} catch (InstantiationException e) {
throw new MockitoException("InstantiationException (see the stack trace for cause): " + e.toString(), e);
} catch (IllegalAccessException e) {
throw new MockitoException("IllegalAccessException (see
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> the stack trace for cause): " + e.toString(), e);
} finally {
if(constructor != null) {
changer.safelyDisableAccess(constructor);
}
}
}
}
/**
* Constructor instantiating strategy for parameterized constructors.
*
* <p>
* Choose the constructor with the highest number of parameters, then
* call the ConstructorArgResolver to get actual argument instances.
* If the argResolver fail, then a technical MockitoException is thrown is thrown.
* Otherwise the instance is created with the resolved arguments.
* </p>
*/
static class ParameterizedConstructorInstantiator implements ConstructorInstantiator {
private final Object testClass;
private final Field field;
private final ConstructorArgumentResolver argResolver;
private final MockUtil mockUtil = new MockUtil();
private final Comparator<Constructor<?>> byParameterNumber = new Comparator<Constructor<?>>() {
public int compare(Constructor<?> constructorA, Constructor<?> constructorB) {
int argLengths = constructorB.getParameterTypes().length - constructorA.getParameterTypes().length;
if (argLengths == 0) {
int constructorAMockableParamsSize = countMockableParams(constructorA);
int constructorBMockableParamsSize = countMockableParams(constructorB);
return constructorBMockableParamsSize - constructorAMockableParamsSize;
}
return argLengths;
}
private int countMockableParams(Constructor<?> constructor) {
int constructorMockableParamsSize = 0;
for (Class<?> aClass : constructor.getParameterTypes()) {
if(mockUtil.isTypeMockable(aClass)){
constructorMockableParamsSize++;
}
}
return constructorMockableParamsSize;
}
};
/**
* Internal, checks are done by FieldInitializer.
* Fields are assumed to be accessible.
*/
ParameterizedConstructorInstantiator(Object testClass, Field field, ConstructorArgumentResolver argumentResolver) {
this.testClass = testClass;
this.field = field;
this.argResolver = argumentResolver;
}
public FieldInitializationReport instantiate() {
final AccessibilityChanger changer = new AccessibilityChanger();
Constructor<?> constructor = null;
try {
constructor = biggestConstructor(field.getType());
changer.enableAccess(constructor);
final Object[] args = argResolver.resolveTypeInstances(
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>constructor.getParameterTypes());
Object newFieldInstance = constructor.newInstance(args);
new FieldSetter(testClass, field).set(newFieldInstance);
return new FieldInitializationReport(field.get(testClass), false, true);
} catch (IllegalArgumentException e) {
throw new MockitoException("internal error : argResolver provided incorrect types for constructor " + constructor + " of type " + field.getType().getSimpleName(), e);
} catch (InvocationTargetException e) {
throw new MockitoException("the constructor of type '" + field.getType().getSimpleName() + "' has raised an exception (see the stack trace for cause): " + e.getTargetException().toString(), e);
} catch (InstantiationException e) {
throw new MockitoException("InstantiationException (see the stack trace for cause): " + e.toString(), e);
} catch (IllegalAccessException e) {
throw new MockitoException("IllegalAccessException (see the stack trace for cause): " + e.toString(), e);
} finally {
if(constructor != null) {
changer.safelyDisableAccess(constructor);
}
}
}
private void checkParameterized(Constructor<?> constructor, Field field) {
if(constructor.getParameterTypes().length == 0) {
throw new MockitoException("the field " + field.getName() + " of type " + field.getType() + " has no parameterized constructor");
}
}
private Constructor<?> biggestConstructor(Class<?> clazz) {
final List<Constructor<?>> constructors = Arrays.asList(clazz.getDeclaredConstructors());
Collections.sort(constructors, byParameterNumber);
Constructor<?> constructor = constructors.get(0);
checkParameterized(constructor, field);
return constructor;
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.cglib;
import org.mockito.cglib.proxy.MethodInterceptor;
import org.mockito.cglib.proxy.MethodProxy;
import org.mockito.internal.InternalMockHandler;
import org.mockito.internal.creation.DelegatingMethod;
import org.mockito.internal.creation.util.MockitoMethodProxy;
import org.mockito.internal.invocation.InvocationImpl;
import org.mockito.internal.invocation.MockitoMethod;
import org.mockito.internal.invocation.SerializableMethod;
import org.mockito.internal.invocation.realmethod.CleanTraceRealMethod;
import org.mockito.internal.progress.SequenceNumber;
import org.mockito.internal.util.ObjectMethodsGuru;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.MockHandler;
import org.mockito.mock.MockCreationSettings;
import java.io.Serializable;
import java.lang.reflect.Method;
/**
* Should be one instance per mock instance, see CglibMockMaker.
*/
class MethodInterceptorFilter implements MethodInterceptor, Serializable {
private static final long serialVersionUID = 6182795666612683784L;
private final InternalMockHandler handler;
final ObjectMethodsGuru objectMethodsGuru = new ObjectMethodsGuru();
private final MockCreationSettings mockSettings;
private final AcrossJVMSerializationFeature acrossJVMSerializationFeature = new AcrossJVMSerializationFeature();
public MethodInterceptorFilter(InternalMockHandler handler, MockCreationSettings mockSettings) {
this.handler = handler;
this.mockSettings = mockSettings;
}
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
if (objectMethodsGuru.isEqualsMethod(method)) {
return proxy == args[0];
} else if (objectMethodsGuru.isHashCodeMethod(method)) {
return hashCodeForMock(proxy);
} else if (acrossJVMSerializationFeature.isWriteReplace(method)) {
return acrossJVMSerializationFeature.writeReplace(proxy);
}
MockitoMethodProxy mockitoMethod
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>Proxy = createMockitoMethodProxy(methodProxy);
new CGLIBHacker().setMockitoNamingPolicy(methodProxy);
MockitoMethod mockitoMethod = createMockitoMethod(method);
CleanTraceRealMethod realMethod = new CleanTraceRealMethod(mockitoMethodProxy);
Invocation invocation = new InvocationImpl(proxy, mockitoMethod, args, SequenceNumber.next(), realMethod);
return handler.handle(invocation);
}
public MockHandler getHandler() {
return handler;
}
private int hashCodeForMock(Object mock) {
return System.identityHashCode(mock);
}
public MockitoMethodProxy createMockitoMethodProxy(MethodProxy methodProxy) {
if (mockSettings.isSerializable())
return new SerializableMockitoMethodProxy(methodProxy);
return new DelegatingMockitoMethodProxy(methodProxy);
}
public MockitoMethod createMockitoMethod(Method method) {
if (mockSettings.isSerializable()) {
return new SerializableMethod(method);
} else {
return new DelegatingMethod(method);
}
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>
* Partial mock usually means that the complexity has been moved to a different method on the same object.
* In most cases, this is not the way you want to design your application.
* <p>
* However, there are rare cases when partial mocks come handy:
* dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.)
* However, I wouldn't use partial mocks for new, test-driven & well-designed code.
* <p>
* Enough warnings about partial mocks, see an example how spiedInstance() works:
* <pre class="code"><code class="java">
* Foo foo = mock(Foo.class, withSettings().spiedInstance(fooInstance));
*
* //Below does exactly the same:
* Foo foo = spy(fooInstance);
* </code></pre>
*
* About stubbing for a partial mock, as it is a spy it will always call the real method, unless you use the
* <code>doReturn</code>|<code>Throw</code>|<code>Answer</code>|<code>CallRealMethod</code> stubbing style. Example:
*
* <pre class="code"><code class="java">
* List list = new LinkedList();
* List spy = spy(list);
*
* //Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty)
* when(spy.get(0)).thenReturn("foo");
*
* //You have to use doReturn() for stubbing
* doReturn("foo").when(spy).get(0);
* </code>
*
* @param instance to spy on
* @return settings instance so that you can fluently specify other settings
*/
MockSettings spiedInstance(Object instance);
/**
* Specifies default answers to interactions.
* It's quite advanced feature and typically you don't need it to write decent tests.
* However it can be helpful when working with legacy systems.
* <p>
* It is the default answer so it will be used <b>only when you don't</b> stub the method call.
*
* <pre class="code"><code class="java">
* Foo mock = mock
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> <p>
* Example:
* <pre class="code"><code class="java">
* //Robust API, via settings builder:
* OtherAbstract spy = mock(OtherAbstract.class, withSettings()
* .useConstructor().defaultAnswer(CALLS_REAL_METHODS));
*
* //Mocking a non-static inner abstract class:
* InnerAbstract spy = mock(InnerAbstract.class, withSettings()
* .useConstructor().outerInstance(outerInstance).defaultAnswer(CALLS_REAL_METHODS));
* </code></pre>
*
* @return settings instance so that you can fluently specify other settings
* @since 1.10.12
*/
@Incubating
MockSettings useConstructor();
/**
* Makes it possible to mock non-static inner classes in conjunction with {@link #useConstructor()}.
* <p>
* Example:
* <pre class="code"><code class="java">
* InnerClass mock = mock(InnerClass.class, withSettings()
* .useConstructor().outerInstance(outerInstance).defaultAnswer(CALLS_REAL_METHODS));
* </code></pre>
*
* @return settings instance so that you can fluently specify other settings
* @since 1.10.12
*/
@Incubating
MockSettings outerInstance(Object outerClassInstance);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.cglib;
import org.mockito.cglib.proxy.MethodProxy;
import java.io.Serializable;
import java.lang.reflect.Field;
class CGLIBHacker {
public void setMockitoNamingPolicy(MethodProxy methodProxy) {
try {
Field createInfoField = reflectOnCreateInfo(methodProxy);
createInfoField.setAccessible(true);
Object createInfo = createInfoField.get(methodProxy);
Field namingPolicyField = createInfo.getClass().getDeclaredField("namingPolicy");
namingPolicyField.setAccessible(true);
if (namingPolicyField.get(createInfo) == null) {
namingPolicyField.set(createInfo, MockitoNamingPolicy.INSTANCE);
}
} catch (Exception e) {
throw new RuntimeException(
"Unable to set MockitoNamingPolicy on cglib generator which creates FastClasses", e);
}
}
@SuppressWarnings("unchecked")
private Field reflectOnCreateInfo(MethodProxy methodProxy) throws SecurityException, NoSuchFieldException {
Class cglibMethodProxyClass = methodProxy.getClass();
// in case methodProxy was extended by user, let's traverse the object
// graph to find the cglib methodProxy
// with all the fields we would like to change
while (cglibMethodProxyClass != MethodProxy.class) {
cglibMethodProxyClass = methodProxy.getClass().getSuperclass();
}
return cglibMethodProxyClass.getDeclaredField("createInfo");
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> (wanted.matches(invocation)) {
firstChunk.add(invocation);
} else if (!firstChunk.isEmpty()) {
break;
}
}
return firstChunk;
}
public Invocation findFirstMatchingUnverifiedInvocation( List<Invocation> invocations, InvocationMatcher wanted, InOrderContext context ){
for( Invocation invocation : removeVerifiedInOrder( invocations, context )){
if( wanted.matches( invocation )){
return invocation;
}
}
return null;
}
public Invocation findSimilarInvocation(List<Invocation> invocations, InvocationMatcher wanted) {
Invocation firstSimilar = null;
for (Invocation invocation : invocations) {
if (!wanted.hasSimilarMethod(invocation)) {
continue;
}
if (firstSimilar == null) {
firstSimilar = invocation;
}
if (wanted.hasSameMethod(invocation)) {
return invocation;
}
}
return firstSimilar;
}
public Invocation findFirstUnverified(List<Invocation> invocations) {
return findFirstUnverified(invocations, null);
}
Invocation findFirstUnverified(List<Invocation> invocations, Object mock) {
for (Invocation i : invocations) {
boolean mockIsValid = mock == null || mock == i.getMock();
if (!i.isVerified() && mockIsValid) {
return i;
}
}
return null;
}
public Location getLastLocation(List<Invocation> invocations) {
if (invocations.isEmpty()) {
return null;
} else {
Invocation last = invocations.get(invocations.size() - 1);
return last.getLocation();
}
}
public Invocation findPreviousVerifiedInOrder(List<Invocation> invocations, InOrderContext context) {
LinkedList<Invocation> verifiedOnly = ListUtil.filter(invocations, new RemoveUnverifiedInOrder(context));
if (verifiedOnly.isEmpty()) {
return null;
} else {
return verifiedOnly.getLast();
}
}
private List<Invocation> removeVerifiedInOrder(List<Invocation> invocations, InOrderContext orderingContext) {
List<Invocation> unverified = new LinkedList<Invocation>();
for (Invocation i : invocations) {
if (orderingContext.isVerified(i)) {
unverified.clear();
} else {
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation;
import org.mockito.MockSettings;
import org.mockito.exceptions.Reporter;
import org.mockito.internal.creation.settings.CreationSettings;
import org.mockito.internal.debugging.VerboseMockInvocationLogger;
import org.mockito.internal.util.MockCreationValidator;
import org.mockito.internal.util.MockNameImpl;
import org.mockito.listeners.InvocationListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.mock.MockName;
import org.mockito.mock.SerializableMode;
import org.mockito.stubbing.Answer;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.mockito.internal.util.collections.Sets.newSet;
@SuppressWarnings("unchecked")
public class MockSettingsImpl<T> extends CreationSettings<T> implements MockSettings, MockCreationSettings<T> {
private static final long serialVersionUID = 4475297236197939569L;
private boolean useConstructor;
private Object outerClassInstance;
public MockSettings serializable() {
return serializable(SerializableMode.BASIC);
}
public MockSettings serializable(SerializableMode mode) {
this.serializableMode = mode;
return this;
}
public MockSettings extraInterfaces(Class... extraInterfaces) {
if (extraInterfaces == null || extraInterfaces.length == 0) {
new Reporter().extraInterfacesRequiresAtLeastOneInterface();
}
for (Class i : extraInterfaces) {
if (i == null) {
new Reporter().extraInterfacesDoesNotAcceptNullParameters();
} else if (!i.isInterface()) {
new Reporter().extraInterfacesAcceptsOnlyInterfaces(i);
}
}
this.extraInterfaces = newSet(extraInterfaces);
return this;
}
public MockName getMockName() {
return mockName;
}
public Set<Class> getExtraInterfaces() {
return extraInterfaces;
}
public Object getSpiedInstance() {
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>
return spiedInstance;
}
public MockSettings name(String name) {
this.name = name;
return this;
}
public MockSettings spiedInstance(Object spiedInstance) {
this.spiedInstance = spiedInstance;
return this;
}
public MockSettings defaultAnswer(Answer defaultAnswer) {
this.defaultAnswer = defaultAnswer;
if (defaultAnswer == null) {
new Reporter().defaultAnswerDoesNotAcceptNullParameter();
}
return this;
}
public Answer<Object> getDefaultAnswer() {
return defaultAnswer;
}
public MockSettingsImpl stubOnly() {
this.stubOnly = true;
return this;
}
public MockSettings useConstructor() {
this.useConstructor = true;
return this;
}
public MockSettings outerInstance(Object outerClassInstance) {
this.outerClassInstance = outerClassInstance;
return this;
}
public boolean isUsingConstructor() {
return useConstructor;
}
public Object getOuterClassInstance() {
return outerClassInstance;
}
public boolean isStubOnly() {
return this.stubOnly;
}
public MockSettings verboseLogging() {
if (!invocationListenersContainsType(VerboseMockInvocationLogger.class)) {
invocationListeners(new VerboseMockInvocationLogger());
}
return this;
}
public MockSettings invocationListeners(InvocationListener... listeners) {
if (listeners == null || listeners.length == 0) {
new Reporter().invocationListenersRequiresAtLeastOneListener();
}
for (InvocationListener listener : listeners) {
if (listener == null) {
new Reporter().invocationListenerDoesNotAcceptNullParameters();
}
this.invocationListeners.add(listener);
}
return this;
}
private boolean invocationListenersContainsType(Class<?> clazz) {
for (InvocationListener listener : invocationListeners) {
if (listener.getClass().equals(clazz)) {
return true;
}
}
return false;
}
public List<InvocationListener> getInvocationListeners() {
return this.invocationListeners;
}
public boolean hasInvocationListeners() {
return !invocationListeners.isEmpty();
}
public Class<T> getTypeToMock() {
return typeToMock;
}
public MockCreationSettings<T> confirm(Class<T
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>> typeToMock) {
return validatedSettings(typeToMock, this);
}
private static <T> CreationSettings<T> validatedSettings(Class<T> typeToMock, CreationSettings<T> source) {
MockCreationValidator validator = new MockCreationValidator();
validator.validateType(typeToMock);
validator.validateExtraInterfaces(typeToMock, source.getExtraInterfaces());
validator.validateMockedType(typeToMock, source.getSpiedInstance());
//TODO SF - add this validation and also add missing coverage
// validator.validateDelegatedInstance(classToMock, settings.getDelegatedInstance());
validator.validateSerializable(typeToMock, source.isSerializable());
validator.validateConstructorUse(source.isUsingConstructor(), source.getSerializableMode());
//TODO SF - I don't think we really need CreationSettings type
CreationSettings<T> settings = new CreationSettings<T>(source);
settings.setMockName(new MockNameImpl(source.getName(), typeToMock));
settings.setTypeToMock(typeToMock);
settings.setExtraInterfaces(prepareExtraInterfaces(source));
return settings;
}
private static Set<Class> prepareExtraInterfaces(CreationSettings settings) {
Set<Class> interfaces = new HashSet<Class>(settings.getExtraInterfaces());
if(settings.isSerializable()) {
interfaces.add(Serializable.class);
}
return interfaces;
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.handler;
import org.mockito.exceptions.Reporter;
import org.mockito.internal.InternalMockHandler;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.invocation.MatchersBinder;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.progress.ThreadSafeMockingProgress;
import org.mockito.internal.stubbing.*;
import org.mockito.internal.verification.MockAwareVerificationMode;
import org.mockito.internal.verification.VerificationDataImpl;
import org.mockito.invocation.Invocation;
import org.mockito.mock.MockCreationSettings;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.VoidMethodStubbable;
import org.mockito.verification.VerificationMode;
import java.util.List;
/**
* Invocation handler set on mock objects.
*
* @param <T>
* type of mock object to handle
*/
class MockHandlerImpl<T> implements InternalMockHandler<T> {
private static final long serialVersionUID = -2917871070982574165L;
InvocationContainerImpl invocationContainerImpl;
MatchersBinder matchersBinder = new MatchersBinder();
MockingProgress mockingProgress = new ThreadSafeMockingProgress();
private final MockCreationSettings mockSettings;
public MockHandlerImpl(MockCreationSettings mockSettings) {
this.mockSettings = mockSettings;
this.mockingProgress = new ThreadSafeMockingProgress();
this.matchersBinder = new MatchersBinder();
this.invocationContainerImpl = new InvocationContainerImpl(mockingProgress, mockSettings);
}
public Object handle(Invocation invocation) throws Throwable {
if (invocationContainerImpl.hasAnswersForStubbing()) {
// stubbing voids with stubVoid() or doAnswer() style
InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(
mockingProgress.getArgumentMatcherStorage(),
invocation
);
invocationContainerImpl.setMethodForStubbing(invocationMatcher);
return null;
}
VerificationMode verificationMode =
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> mockingProgress.pullVerificationMode();
InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(
mockingProgress.getArgumentMatcherStorage(),
invocation
);
mockingProgress.validateState();
// if verificationMode is not null then someone is doing verify()
if (verificationMode != null) {
// We need to check if verification was started on the correct mock
// - see VerifyingWithAnExtraCallToADifferentMockTest (bug 138)
if (((MockAwareVerificationMode) verificationMode).getMock() == invocation.getMock()) {
VerificationDataImpl data = createVerificationData(invocationContainerImpl, invocationMatcher);
verificationMode.verify(data);
return null;
} else {
// this means there is an invocation on a different mock. Re-adding verification mode
// - see VerifyingWithAnExtraCallToADifferentMockTest (bug 138)
mockingProgress.verificationStarted(verificationMode);
}
}
// prepare invocation for stubbing
invocationContainerImpl.setInvocationForPotentialStubbing(invocationMatcher);
OngoingStubbingImpl<T> ongoingStubbing = new OngoingStubbingImpl<T>(invocationContainerImpl);
mockingProgress.reportOngoingStubbing(ongoingStubbing);
// look for existing answer for this invocation
StubbedInvocationMatcher stubbedInvocation = invocationContainerImpl.findAnswerFor(invocation);
if (stubbedInvocation != null) {
stubbedInvocation.captureArgumentsFrom(invocation);
return stubbedInvocation.answer(invocation);
} else {
Object ret = mockSettings.getDefaultAnswer().answer(invocation);
// redo setting invocation for potential stubbing in case of partial
// mocks / spies.
// Without it, the real method inside 'when' might have delegated
// to other self method and overwrite the intended stubbed method
// with a different one. The reset is required to avoid runtime exception that validates return type with stubbed method signature.
invocationContainerImpl.resetInvocationForPotentialStubbing(invocationMatcher);
return ret;
}
}
public VoidMethodStubbable<T> voidMethodStubbable(T mock) {
return new VoidMethodStubbableImpl<T>(mock, invocationContainerImpl);
}
public MockCreationSettings getMockSettings() {
return mockSettings;
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection;
import java.lang.reflect.Field;
import java.util.Set;
/**
* Injector strategy contract
*/
public abstract class MockInjectionStrategy {
/**
* NOP Strategy that will always try the next strategy.
*/
public static final MockInjectionStrategy nop() {
return new MockInjectionStrategy() {
protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
return false;
}
};
}
private MockInjectionStrategy nextStrategy;
/**
* Enqueue next injection strategy.
*
* <p>
* The implementation should take care of the actual calling if required.
* </p>
*
* @param strategy Queued strategy.
* @return The passed strategy instance to allow chaining.
*/
public MockInjectionStrategy thenTry(MockInjectionStrategy strategy) {
if(nextStrategy != null) {
nextStrategy.thenTry(strategy);
} else {
nextStrategy = strategy;
}
return strategy;
}
/**
* Actually inject mockCandidates on field.
*
* <p>
* Actual algorithm is defined in the implementations of {@link #processInjection(Field, Object, Set)}.
* However if injection occurred successfully, the process should return <code>true</code>,
* and <code>false</code> otherwise.
* </p>
*
* <p>
* The code takes care of calling the next strategy if available and if of course if required
* </p>
*
* @param onField Field needing injection.
* @param fieldOwnedBy The owning instance of the field.
* @param mockCandidates A set of mock candidate, that might be injected.
* @return <code>true</code> if successful, <code>false</code> otherwise.
*/
public boolean process(Field onField, Object fieldOwnedBy, Set<Object> mockCandidates) {
if(processInjection(onField, fieldOwnedBy, mockCandidates)) {
return true;
}
return relayProcessToNextStrategy(onField, fieldOwnedBy, mockCandidates);
}
/**
*
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS> Process actual injection.
*
* <p>
* Don't call this method directly, instead call {@link #process(Field, Object, Set)}
* </p>
*
* @param field Field needing injection
* @param fieldOwner Field owner instance.
* @param mockCandidates Pool of mocks to inject.
* @return <code>true</code> if injection occurred, <code>false</code> otherwise
*/
protected abstract boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates);
private boolean relayProcessToNextStrategy(Field field, Object fieldOwner, Set<Object> mockCandidates) {
return nextStrategy != null && nextStrategy.process(field, fieldOwner, mockCandidates);
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.matchers;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;
@SuppressWarnings("unchecked")
public class NewMatchersTest extends TestBase {
private IMethods mock;
@Before
public void setUp() {
mock = Mockito.mock(IMethods.class);
}
@Test
public void shouldAllowAnyList() {
when(mock.forList(anyList())).thenReturn("matched");
assertEquals("matched", mock.forList(Arrays.asList("x", "y")));
assertEquals(null, mock.forList(null));
verify(mock, times(1)).forList(anyList());
}
@Test
public void shouldAllowAnyCollection() {
when(mock.forCollection(anyCollection())).thenReturn("matched");
assertEquals("matched", mock.forCollection(Arrays.asList("x", "y")));
assertEquals(null, mock.forCollection(null));
verify(mock, times(1)).forCollection(anyCollection());
}
@Test
public void shouldAllowAnyMap() {
when(mock.forMap(anyMap())).thenReturn("matched");
assertEquals("matched", mock.forMap(new HashMap<String, String>()));
assertEquals(null, mock.forMap(null));
verify(mock, times(1)).forMap(anyMap());
}
@Test
public void shouldAllowAnySet() {
when(mock.forSet(anySet())).thenReturn("matched");
assertEquals("matched", mock.forSet(new HashSet<String>()));
assertEquals(null, mock.forSet(null));
verify(mock, times(1)).forSet(anySet());
}
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito;
public interface MockitoDebugger {
//Prints all interactions with mock. Also prints stubbing information.
//You can put it in your 'tearDown' method
String printInvocations(Object ... mocks);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.matchers;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;
@SuppressWarnings("unchecked")
public class AnyXMatchersAcceptNullsTest extends TestBase {
private IMethods mock;
@Before
public void setUp() {
mock = Mockito.mock(IMethods.class);
}
@Test
public void shouldNotAcceptNullInAnyXMatchers() {
when(mock.oneArg(anyObject())).thenReturn("0");
when(mock.oneArg(anyString())).thenReturn("1");
when(mock.forList(anyList())).thenReturn("2");
when(mock.forMap(anyMap())).thenReturn("3");
when(mock.forCollection(anyCollection())).thenReturn("4");
when(mock.forSet(anySet())).thenReturn("5");
assertEquals(null, mock.oneArg((Object) null));
assertEquals(null, mock.oneArg((String) null));
assertEquals(null, mock.forList(null));
assertEquals(null, mock.forMap(null));
assertEquals(null, mock.forCollection(null));
assertEquals(null, mock.forSet(null));
}
@Test
public void shouldNotAcceptNullInAllAnyPrimitiveWrapperMatchers() {
when(mock.forInteger(anyInt())).thenReturn("0");
when(mock.forCharacter(anyChar())).thenReturn("1");
when(mock.forShort(anyShort())).thenReturn("2");
when(mock.forByte(anyByte())).thenReturn("3");
when(mock.forBoolean(anyBoolean())).thenReturn("4");
when(mock.forLong(anyLong())).thenReturn("5");
when(mock.forFloat(anyFloat())).thenReturn("6");
when(mock.forDouble(anyDouble())).thenReturn("7");
assertEquals(null, mock.forInteger(null));
assertEquals(null, mock.forCharacter(null));
assertEquals(null, mock.for
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
* Simple annotation processor interface.
*/
public interface FieldAnnotationProcessor<A extends Annotation> {
Object process(A annotation, Field field);
}
Mockito, 6
<FILEB>
<CHANGES>
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Character.class)).returnChar();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Long.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Float.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Double.class)).returnZero();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Short.class)).returnZero();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
<CHANGEE>
<CHANGES>
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
<CHANGEE>
<CHANGES>
return (T) reportMatcher(Any.ANY).returnNull();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(String.class)).returnString();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(List.class)).returnList();
<CHANGEE>
<CHANGES>
return anyList();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Set.class)).returnSet();
<CHANGEE>
<CHANGES>
return anySet();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Map.class)).returnMap();
<CHANGEE>
<CHANGES>
return anyMap();
<CHANGEE>
<CHANGES>
return reportMatcher(new InstanceOf(Collection.class)).returnList();
<CHANGEE>
<CHANGES>
return anyCollection();
<CHANGEE>
<FILEE>
<FILEB>
* For example, if custom argument matcher is not likely to be reused
* or you just need it to assert on argument values to complete verification of behavior.
*/
@SuppressWarnings("unchecked")
public class Matchers {
private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
/**
* Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
<CHANGES>
return reportMatcher(Any.ANY).returnFalse();
<CHANGEE>
}
/**
* Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static byte anyByte() {
<CHANGES>
return reportMatcher(Any.ANY).returnZero();
<CHANGEE>
}
/**
* Any <code>char</code>, <code>Character</code> or <code>null</code>.
* <p>
* This method <b>*doesn't do any type checks*</b>, it is only there to avoid casting
* in your code. This might however change (type checks could be added) in a
* future major release.
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>0</code>.
*/
public static char anyChar() {
<CHANGES>
return reportMatcher(Any.ANY).returnChar();
<CHANGEE>
}
/**
* Any int, Integer or <code>null</code>.
* <p>
*<SCANS>/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.invocation;
/**
* A method call on a mock object. Contains all information and state needed for the Mockito framework to operate.
* This API might be useful for developers who extend Mockito.
* <p>
* The javadoc does not have lots of examples or documentation because its audience is different.
* Vast majority of users don't need to use the Invocation. It's mostly useful for other framework authors
* that extend Mockito.
*
* @since 1.9.5
*/
public interface Invocation extends InvocationOnMock, DescribedInvocation {
/**
* @return whether the invocation has been already verified.
* Needed for {@link org.mockito.Mockito#verifyNoMoreInteractions(Object...)}
*/
boolean isVerified();
/**
* @return the sequence number of the Invocation. Useful to determine the order of invocations.
* Used by verification in order.
*/
int getSequenceNumber();
/**
* @return the location in code of this invocation.
*/
Location getLocation();
/**
* Returns unprocessed arguments whereas {@link #getArguments()} returns
* arguments already processed (e.g. varargs expended, etc.).
*
* @return unprocessed arguments, exactly as provided to this invocation.
*/
Object[] getRawArguments();
/**
* Marks this invocation as verified so that it will not cause verification error at
* {@link org.mockito.Mockito#verifyNoMoreInteractions(Object...)}
*/
void markVerified();
/**
* @return the stubbing information for this invocation. May return null - this means
* the invocation was not stubbed.
*/
StubInfo stubInfo();
/**
* Marks this invocation as stubbed.
*
* @param stubInfo the information about stubbing.
*/
void markStubbed(StubInfo stubInfo);
/**
* Informs if the invocation participates in verify-no-more-invocations or verification in order.
*
* @return whether this invocation should be ignored for the purposes of
* verify-no-more-invocations or verification in order.
*/
boolean isIgnoredForVerification();
/**
* Configures this invocation to